Topic
3 replies Latest Post - ‏2011-02-15T14:04:11Z by camorton
SystemAdmin
SystemAdmin
196 Posts
ACCEPTED ANSWER

Pinned topic Undefined types in inline functions

‏2011-02-10T12:59:29Z |
Hi again,

I'm having problems compiling some code that uses some types that were declared but not yet defined.

From what I have seen, forward declarations should work when you have pointers, references, arguments to methods and such. Now, I have some methods that return objects from these types (not references or pointers) and xlC is complaining they were not defined. The message looks like such:



"file.C", line 10.30: 1540-1118 (S) The declaration of 
"SomeMethod" uses the undefined 

class 
"Template<AnotherClass>" when the 

class must be complete.


The problem is that "AnotherClass" is derived from the class that is defining SomeMethod.

The code is structured somewhat like such:


template <

class T> 

class Template; 

class AnotherClass;   

class SomeClass 
{ inline Template<AnotherClass> SomeMethod( ) 

const; 
};   

class AnotherClass: 

public SomeClass 
{ 
};


Any ideas on how to fix this?
Updated on 2011-02-15T14:04:11Z at 2011-02-15T14:04:11Z by camorton
  • SystemAdmin
    SystemAdmin
    196 Posts
    ACCEPTED ANSWER

    Re: Undefined types in inline functions

    ‏2011-02-11T12:36:59Z  in response to SystemAdmin
    Ok, putting it in another way:

    Does the following code:

    inline Template<SomeClass> aMethod( ) { return SomeOtherClass::AStaticMethod(); }
    


    require the definition of both Template and SomeClass, as we are returning the object by value?
    Updated on 2014-03-24T22:17:30Z at 2014-03-24T22:17:30Z by iron-man
  • camorton
    camorton
    10 Posts
    ACCEPTED ANSWER

    Re: Undefined types in inline functions

    ‏2011-02-15T13:55:15Z  in response to SystemAdmin
    In order to instantiate a template class, the class must be complete at the point of instantiation.

    In your example, the template for the return type will be instantiated when the function is called, so all the classes need to be complete when the function is called

    template <class T> class Template;
    class AnotherClass;
     
    struct SomeClass {
        Template<AnotherClass> SomeMethod( ) const;
    };
     
    class AnotherClass: public SomeClass {
    };
     
    template <class T> class Template{};
     
    int main(void)
      {
       AnotherClass a;
       a.SomeMethod();
      }
    
    Updated on 2014-03-24T22:17:20Z at 2014-03-24T22:17:20Z by iron-man
  • camorton
    camorton
    10 Posts
    ACCEPTED ANSWER

    Re: Undefined types in inline functions

    ‏2011-02-15T14:04:11Z  in response to SystemAdmin
    Sorry I posted before finishing my examples:

    However, the inline definition causes the compiler to evaluate the return type sooner causing the error.

    
    template <
    
    class T> 
    
    class Template; 
    
    class AnotherClass; struct SomeClass 
    { Template<AnotherClass> SomeMethod( ) 
    
    const
    {
    }; 
    }; 
    
    class AnotherClass: 
    
    public SomeClass 
    { 
    };   template <
    
    class T> 
    
    class Template
    {
    };   
    
    int main(
    
    void) 
    { AnotherClass a; a.SomeMethod(); 
    }
    


    
    
    "y1.cpp", line 5.28: 1540-1118 (S) The declaration of 
    "SomeMethod" uses the undefined 
    
    class 
    "Template<AnotherClass>" when the 
    
    class must be complete.
    


    In this case you would need the classes for the return type before the method is defined.

    So if you define the function after the classes are complete, all classes will be ready when the compiler evaluates the return type.

    
    template <
    
    class T> 
    
    class Template; 
    
    class AnotherClass; struct SomeClass 
    { inline Template<AnotherClass> SomeMethod( ) 
    
    const; 
    }; 
    
    class AnotherClass: 
    
    public SomeClass 
    { 
    };   template <
    
    class T> 
    
    class Template
    {
    };   inline Template<AnotherClass> SomeClass::SomeMethod() 
    
    const 
    {
    }   
    
    int main(
    
    void) 
    { AnotherClass a; a.SomeMethod(); 
    }