Thread: unresolved external, template class prob

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    unresolved external, template class prob

    When compiling this code I get an unresolved external symbol "public: static class Outer<int>::Inner Outer<int>::inner" with VC6 and "undefined reference to `Outer<int>::inner' " with g++. Perhaps Im missing something obvious but I cant figure it out. Any ideas?

    Code:
    template <class T>
    class Outer {
      class Inner
      {
        public: 
          void f() {} 
      };
      public:
        static Inner inner;
    };
    
    int main()
    {
      Outer<int>::inner.f();
      return 0;
    }
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I don't see anything obvious either. Perhaps Inner needs to be a templated class aswell.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Because you declare a static member variable but you never define it. This line:

    static Inner inner;

    does NOT allocate space for an object of class Outer::Inner. It only says to the compiler "I promise that in some source file, I will define Outer<T>::inner.

    One issue here is that you have templatized this class; static members and templates mix poorly because of the convoluted way you must both declare and later define the static member. The correct code is:

    Code:
    template <class T>
    class Outer {
      class Inner
      {
        public: 
          void f() {} 
      };
      public:
        static Inner inner;
    };
    
    int main()
    {
      Outer<int>::inner.f();
      return 0;
    }
    
    Outer<int>::Inner Outer<int>::inner;
    The bad news is that you need to explicitly allocate that static variable, and you can't templatize it. So you should try to do without a static data member if you can possibly avoid it.

    It's the same reason that:

    Code:
    class C{
    public:
      static int c;
    };
    
    int main(){
      C::c = 5; // causes a link error, C::c doesn't exist.
    }
    is a linker error (you declare a static int to exist but don't actually make it happen), while:

    Code:
    class C{
    public:
      static int c;
    };
    
    int C::c;
    
    int main(){
      C::c = 5;
    }
    is OK.
    Last edited by Cat; 07-18-2003 at 10:24 PM.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Thanks Cat!

    code involving templates are always giving me the biggest headaches
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 01:46 AM
  3. Template class and unresolved externals..!
    By Halloko in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2005, 06:28 PM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. Ask about these "unresolved external symbol" error
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2002, 11:39 AM