Thread: Linking error when using static member variable in templated class (MSVC++ 6.0)

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    Linking error when using static member variable in templated class (MSVC++ 6.0)

    I am making a templated class for a linked list in Visual C++ 6.0. Everything works fine until I put the static keyword in front of "int length;" I get the following error "error LNK2001: unresolved external symbol "private: static int LList<int>::length" (?length@?$LList@H@@0HA)"

    I have a feeling that it is the templating that is causing the problem because there is an example in the MSDN help files for the static keyword that does exactly this except the class isn't templated. However, I have no idea why this error is occuring.

    The following is the class interface(comments stripped):

    Code:
    template<class T> 
    class LList
    {
    public:
    	LList();
    	LList(T firstItem);
    	void addItem(T item);	
    	int removeItem(T item);	
    	bool proveItem(T item);	
    	T findItemByPos(int pos);	
    	void editItem(T item, T newItem);
    	void editItemByPos(int pos, T newItem);
    	void printInt();		
    
    
    private:
    	static int length;  //<--Here is the problem
    	T data;
    	LList* next;
    
    };

    Anyone know why this is happening?

    Thanks,
    Chris

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Are you sure you want it to be static? That would mean there is only one length for every LList<int> in your program (all list of ints would have to have the same "size")... sounds illogical.

    You need to declare the length member as it is static for every type of list you want to use. IE,

    Code:
    #include "LList.h"
    LList<int>::size = 0;
    LList<double>::size = 0;
    
    int main() {
         LList<int> something;
         LList<double> blah;
         return 0;
    }
    Warning, I did not test the code.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Should be

    Code:
    int <T> LList::length = 0;

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Originally posted by SilentStrike
    Are you sure you want it to be static? That would mean there is only one length for every LList<int> in your program (all list of ints would have to have the same "size")... sounds illogical.
    You are absolutely correct... That is completely illogical. I realized that last night about a half an hour after I posted.

    Thanks for the replies though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error with static template class members
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2009, 12:31 PM
  2. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM
  5. Declaration/Definition ??
    By Manish in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:37 AM