Thread: compiler happy but undefined symbol in GCC

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    compiler happy but undefined symbol in GCC

    Can someone please explain how to correct this code which is getting an error when build with gcc

    ==
    Code:
    template <class T> class Test {
    public:
        class SubClass {
        public:
            SubClass() : i(1) {}
            int i;
        };
        static SubClass statics;
    };
    
    template<> Test<int> :: SubClass Test<int> :: statics;
    
    int main(int argc, char* const argv[])
    {
        int j = Test<int> :: statics.i;
        return 0;
    }
    ==

    error message from gcc is

    C:\Users\Roger\AppData\Local\Temp/cc0oeaaa.o:bug3.cpp.text+0x17):
    undefined reference to `Test<int>::statics'

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    I've "fixed" it with this 'hack'

    ==
    Code:
    template <class T> class Test {
    public:
    	class InnerClass {
    	public:
    		InnerClass(bool) : i(1) {}
    		T i;
    	};
    	static InnerClass statics;
    };
    
    template<> Test<int> :: InnerClass Test<int> :: statics(true);
    
    int main(int argc, char* const argv[])
    {
    	int j = Test<int> :: statics.i;
    	return 0;
    }
    ==

    ie added a dummy bool constructor arg to the InnerClass, and then added constructor args to the definition of statics.

    A bit ugly, but it works

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Soma

    Code:
    template <class T> class Test {
    public:
        class SubClass {
        public:
            SubClass() : i(1) {}
            int i;
        };
        static SubClass statics;
    };
    
    template <typename T> typename Test<T> :: SubClass Test<T> :: statics;
    
    int main(int argc, char* const argv[])
    {
        int j = Test<int> :: statics.i;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  2. Undefined Symbol problem
    By Duskan in forum C Programming
    Replies: 2
    Last Post: 05-20-2007, 04:01 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. whats wrong with this
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2002, 03:40 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM