Thread: Forward declaration owned my ass apparently...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    Forward declaration owned my ass apparently...

    I have a head ache. -.-

    Code:
    class ENTITY;
    class UNIVERSE
    {
    public:
    	void insert_entity(ENTITY *entity)
    	{
    		m_entities.push_back(entity);
    	}
    
    	void tick()
    	{
    		for(std::list<ENTITY*>::iterator i=m_entities.begin(); i != m_entities.end(); ++i)
    		{
    			(*i)->instantiate(); // use of undefined type 'RITZ::PHYS::ENTITY'
    		}
    	}
    
    private:
    	std::list<ENTITY*> m_entities;
    };
    
    class ENTITY
    {
    ...
    };
    It isn't like I've never forward declared a class before... Why is my compiler whining, and why does it have no problem with all the previous uses of the "undefined" class ENTITY.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You can't declare an object of a class until you define it. Forward declaration only allows you to address that you'll have definitions for it later. Until it's defined you can only use the name of the class.
    Code:
    // This is legal
    #include <iostream>
    
    class CLASS2;
    
    class CLASS1 {
       public:
          void myMethod1(CLASS2);
    };
    
    class CLASS2 {
       public:
          void myMethod2() { std::cout << "Inside myMethod2"; }
    };
    
    void CLASS1::myMethod1(CLASS2 c2Obj) { c2Obj.myMethod2(); }
    Code:
    // This is illegal
    #include <iostream>
    
    class CLASS2;
    
    class CLASS1 {
       public:
          void myMethod1(CLASS2 c2Obj) { // c2Obj is an incomplete type
             c2Obj.myMethod2();
       }
    };
    
    class CLASS2 {
       public:
          void myMethod2() { std::cout << "Inside myMethod2"; }
    };
    Last edited by SlyMaelstrom; 03-27-2006 at 12:06 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    This I know. I'm not declaring any instance of the class.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Excuse me for not being positive about std::lists, but isn't
    Code:
    (*i)->instantiate();
    the method instantiate a method of your ENTITY class, not list? That is why you're dereferencing it, correct? I also like that you define your code under the RITZ namespace.
    Last edited by SlyMaelstrom; 03-27-2006 at 12:26 AM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    What's wrong with that? I can't call a member function?
    Last edited by RITZ; 03-27-2006 at 12:29 AM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ...and that's all wonderful, unfortunately, if it's defined in the ENTITY class, the it can't be used before it's declaration. Forward declaration of the class doesn't tell the compile to assume that all methods you address with the class will be defined later.
    Code:
    // This is still illegal
    #include <iostream>
    #include <list>
    
    class CLASS2;
    
    class CLASS1 {
       public:
          void myMethod1() { // c2Obj is an incomplete type
             for(std::list<CLASS2*>::iterator i = c2List.begin(); 
                 i != c2List.end(); ++i)
             (*i)->myMethod2(); // invalid use of undefined type 'struct CLASS2'
       }
       private:
          std::list<CLASS2*> c2List;
    };
    
    class CLASS2 {
       public:
          void myMethod2() { std::cout << "Inside myMethod2"; }
    };
    
    int main() {
        CLASS1 c1Obj;
        
        c1Obj.myMethod1();
        
        return 0;
    }
    Last edited by SlyMaelstrom; 03-27-2006 at 12:35 AM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    Ok, I thought you could still access members. You can use any potential member you want with templates after all and you won't get an error until the code is generated.

    I also like that you define your code under the RITZ namespace.
    The worst is when headers #define constants so you can't even defeat their duplicate naming with a namespace.
    Code:
    namespace N
    {
        const unsigned long INFINITE = 0xffffffff;
    }
    If windows.h is included N::INFINITE becomes literally N::0xffffffff.
    Last edited by RITZ; 03-27-2006 at 12:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM