Thread: Static members.....creating problem

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Static members.....creating problem

    Following is the code I had written for a class list. It contains a static int Pos, which I wanted to initialise.

    Code:
    class list  
    {
    	struct node *head, *tail;
        static int Pos ;
    public:
        list();
    	~list();
    	void enqueue (const int&);
    	int deque () ;
    	int pop() ;
    	struct node* find(const int) const;
    	
    };
    
    int list::Pos = 0;
    However, upon compiling the program I get the following linker error
    Main.obj : error LNK2005: "private: static int list::Pos" (?Pos@list@@0HA) already defined in list.obj
    Debug/Static1.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.


    Where am I redifining the static int Pos ?

    Is this is the wrong way to initialise a static data member ?

    Please help.
    First hear, then understand, and then, leaving all distractions, shut your mind to outside influences and devote yourself to developing the truth within you.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    put this line

    int list::Pos = 0;

    in list.cpp instead of list.h

    you will find that it magically is fixed. The reason is when you have a variable declared in a .h and include it in multiple .cpp files you get both cpp's trying to create the same variable name.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. static data members
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 05:18 PM
  3. are static class members the right choice?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2005, 09:41 AM
  4. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM
  5. static members construction
    By glUser3f in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2003, 09:58 PM