Thread: Class and functions

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Class and functions

    I have this code here...

    Code:
        class MyFrameListener : public Ogre::FrameListener
        {
        	public:
        	
            InputReader* mInputReader = PlatformManager::getSingleton().createInputReader();  //Here
            mInputReader->initialise(mWindow,true,true);  //And Here
            
            virtual bool frameStarted(const FrameEvent& evt)
            {
            	if (mInputReader.isKeyDown(Ogre::KC_ESCAPE))
            	{
            		return false;
            	}
                return true;
            }
    
            virtual bool frameEnded(const FrameEvent& evt)
            {
                return true;
            }
            
            MyFrameListener();
        };
        
        Ogre::FrameListener* listener = new MyFrameListener;
        mRoot->addFrameListener(listener);
        mRoot->startRendering();
    The lines that have Here comments are the ones of internest. I am using Ogre Rendering Engine for my 3d graphics. I also got a new better compiler than dev-c++.

    But anyways, I need away to put those function either in the class, or out side the class. But if they are in I get an error saying I can't do that, if they are out, the class does not recognize the functions.

    Otherwise without those 2 lines of code marked Here, and with the rest of the code, it compiles fine.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Perhaps you want these in the constructor?
    As they are, they are freely floating in space, with no order. These calls belong inside a function.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I would put

    InputReader* mInputReader

    were it is now and than move the actaully code stuff in the constructor? Would that work?

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Looks like Zach has already hinted at the answer.

    I would like to point out the usefulness of separating implementation and definition into different files.

    For instance, you can show all the definitions you would like in MyClassListener.h and define any public variable you want, while implementing it in MyClassListener.cpp. Its very helpful for organization of code and readbility for other people (and yourself later on). In a larger project such as this (or from the looks of it anyway), you should get in the habit of separating them out for your own sanity.

    It looks like you put in the function MyFrameListener();

    so you might consider in MyFrameListener.cpp to have this..
    Code:
    MyFrameListener::MyFrameListener()
    {
    InputReader* mInputReader = PlatformManager::getSingleton().createInputReader(  );  //Here
            mInputReader->initialise(mWindow,true,true);  //And Here
    
    }

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    alright well I am going to organzie later, right now I am trying to get a working frame work made. Also small project... at the moment.

    I'll go that than.

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Code right now

    Code:
        class MyFrameListener : public Ogre::FrameListener
        {
        	public:
        	
            InputReader* mInputReader;
            
            virtual bool frameStarted(const FrameEvent& evt)
            {
                return true;
            }
    
            virtual bool frameEnded(const FrameEvent& evt)
            {
                return true;
            }
            
            MyFrameListener();
        };
        
        MyFrameListener::MyFrameListener()
        {  //Error 1
            mInputReader = PlatformManager::getSingleton().createInputReader();  //Error 2
            mInputReader->initialise(mWindow,true,true);  //Error 3 and 4
        };
    I get these errors:

    1. error C2143: syntax error : missing ';' before '{'
    2. error C2065: 'mInputReader' : undeclared identifier
    3. error C2227: left of '->initialise' must point to class/struct/union
    4. error C3861: 'mInputReader': identifier not found, even with argument-dependent lookup

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Don't see anything in that code (note, you can drop the ; after the definition of the ctor).

    Is there any code above this in your file, or perhaps you include a header you wrote? That is where I tend to see these "missing ; before..." errors (they don't make it easy).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    well I can not remove that ; in the ctor, gives an error.

    The code above it has NOTHING to do with the code I showed you, I made sure and double checked that. I have no .h files, just 1 .cpp file in my project.

    I am not seeing anything either that would invoke those errors. I'm lost.

    EDIT: I also know no other code effects this, cause if I take this code out, it compiles fine.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> well I can not remove that ; in the ctor, gives an error. <<
    Yes, you can remove it.
    The error is not there. It is somewhere above.

    I "filled in the blanks" and make a version of your code, leaving what you wrote intact:
    Code:
    namespace Ogre {
       struct FrameListener { };
    }
    
    struct FrameEvent { };
    
    struct InputReader { 
       void initialise(int, bool, bool) { }
    };
    
    struct PlatformManager {
          static PlatformManager& getSingleton() { return _instance; }
          InputReader* createInputReader() { return new InputReader; }
       private:
          static PlatformManager _instance;
    };
    
    PlatformManager PlatformManager::_instance;
    int mWindow;
    
    class MyFrameListener : public Ogre::FrameListener
    {
       public:
          InputReader* mInputReader;
            
          virtual bool frameStarted(const FrameEvent& evt)
          {
             return true;
          }
    
          virtual bool frameEnded(const FrameEvent& evt)
          {
             return true;
          }
            
          MyFrameListener();
    };
        
    MyFrameListener::MyFrameListener()
    {  //Error 1
       mInputReader = PlatformManager::getSingleton().createInputReader(  );  //Error 2
       mInputReader->initialise(mWindow,true,true);  //Error 3 and 4
    }
    
    int main() {}
    It's not pretty, but it compiles fine. Somewhere else above, there is a problem.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright no one said I was pro at c++ so I got no idea how to put that into my code. But here I will just give FULL code...

    Code:
    #include <windows.h>
    #include <Ogre.h>
     
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
    {
    	using namespace Ogre;
    	using namespace std;
    	
    	//you’ll start inserting your code here
        Root *mRoot;
        mRoot = new Root("plugins.cfg", "display.cfg", "log.txt");
        
        if(! mRoot->showConfigDialog())
        {
            //gotta pick a valid renderer and hit the big Okay button man..
            return 0;
        }
        
        RenderWindow* mWindow;
        mWindow = mRoot->initialise(true, "Some Window Title");
            
        SceneManager* mSceneMgr;
        mSceneMgr = mRoot->getSceneManager(ST_GENERIC);
        
        //Oh a camera, how nice.
        Camera* mCamera;
        mCamera = mSceneMgr->createCamera("PlayerCam");
        // Position it at 500 in Z direction
        mCamera->setPosition(Vector3(0,0,500));
        // Look back along -Z
        mCamera->lookAt(Vector3(0,0,-300));
        mCamera->setNearClipDistance(5);
        
        // Create one viewport, entire window
        Viewport* vp = mWindow->addViewport(mCamera);
        vp->setBackgroundColour(ColourValue(0,0,255));
        
        // Alter the camera aspect ratio to match the viewport
        mCamera->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
        
        
        class MyFrameListener : public Ogre::FrameListener
        {
        	public:
        	
            InputReader* mInputReader;
            
            virtual bool frameStarted(const FrameEvent& evt)
            {
                return true;
            }
    
            virtual bool frameEnded(const FrameEvent& evt)
            {
                return true;
            }
            
            MyFrameListener();
        };
        
        MyFrameListener::MyFrameListener()
        {
            mInputReader = PlatformManager::getSingleton().createInputReader();
            mInputReader->initialise(mWindow,true,true);
        };
        
        Ogre::FrameListener* listener = new MyFrameListener;
        mRoot->addFrameListener(listener);
        mRoot->startRendering();
        
        
    	return 0;
    }
    This is probebly going to the bit extream. And when I take that ; at the constructor out, I get a new error.

    That code compiles just fine without the FrameListener stuff in it. (Of course I got the lib file included so it does compile).

    I am also using Visual Toolkit 2003 (7.1 or somtin like that) for my compiler, Code::Blocks for my IDE, and Windows Platform SDK and MinGW and other stuff for my include files, libs, etc.

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> That code compiles just fine without the FrameListener stuff in it. <<

    Take the class declaration, and all associated function definitions out of the definition of WinMain, and put them (at least the declaration for the class) above it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    You are the best! Wow I such a simple problem.... been long sence I used C++, to used to C#.

    Ok now just one more simple question. What if I want to use the mInputReader in my vitual bool frameStarted function.

    Code:
    class MyFrameListener : public Ogre::FrameListener
    {
        public:
        	
        Ogre::InputReader* mInputReader;
            
    //Right here is were I want to use mInputReader
        virtual bool frameStarted(const FrameEvent& evt, mInputReader input) //Error here
        {
        	if (input.isKeyDown(Ogre::KC_ESCAPE))
        	{
        		return false;
        	}
            return true;
        }
    
        virtual bool frameEnded(const FrameEvent& evt)
        {
            return true;
        }
            
        MyFrameListener(Ogre::RenderWindow* mWindow);
    };
    
    MyFrameListener::MyFrameListener(Ogre::RenderWindow* mWindow)
    {
        mInputReader = Ogre::PlatformManager::getSingleton().createInputReader();
        mInputReader->initialise(mWindow,true,true);
    };
    The way I have it set up says:

    error C2061: syntax error : identifier 'mInputReader'

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    virtual bool frameStarted(const FrameEvent& evt, mInputReader input) //Error here
    What you have is that you are declaring a parameter which takes something of the type mInputReader, and the parameter is named input. The type ought to be Ogre::InputReader. However, as the function is a class member function, you can use mInputReader without having to pass it to the function.


    ... and you can still drop that semi-colon after the function definition.


    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok just ONE (1) more problem...

    Code:
        virtual bool frameStarted(const FrameEvent& evt)
        {
        	if (mInputReader.isKeyDown(Ogre::KC_ESCAPE))  //Error
        	{
        		return false;
        	}
            return true;
        }
    error C2228: left of '.isKeyDown' must have class/struct/union type

    And yes I do try lots of things before I come here for help. Just out of look today or somthing.

    And I tell you I can NOT take away that semicoln ( ; ). But if it can go away like you say (but it can't) why would it hurt having it there? But this is not the problem. That doesn't produce and error or warning.

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> And I tell you I can NOT take away that semicoln ( ; ). But if it can go away like you say (but it can't) why would it hurt having it there? But this is not the problem. That doesn't produce and error or warning.

    Right, it doesn't hurt. But there should be no error / warning if it is removed, so, something is amiss.

    And, mInputReader is a pointer to an object of type InputReader, so use mInputReader->foo() instead of mInputReader.foo().
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM