Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Linker Error with static vector

Options
  • 12-04-2010 2:43pm
    #1
    Closed Accounts Posts: 2,103 ✭✭✭


    In C++ Im trying to create an object that contains a static list. In the constructor im trying to add the this pointer to the list. Im not really used to using static variables so im probably just making a syntax mistake, but can anyone take a look at this code and take a guess as to why it won't compile.
    #ifndef TPRIGIDBODY
    #define TPRIGIDBODY
    
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class tpRigidBody{
    private:
    
    public:
    
        static vector<tpRigidBody * const> *rigidBodyList;
    
        tpRigidBody();
        ~tpRigidBody();
    
    };
    
    #endif // TPRIGIDBODY
    
    #include "tpRigidBody.h"
    
    tpRigidBody::tpRigidBody(){
        rigidBodyList->push_back(this);
    }
    
    tpRigidBody::~tpRigidBody(){
    
    }
    

    the linker error i get when i try to compile is;
    error LNK2001: unresolved external symbol "public: static class std::vector<class tpRigidBody * const,class std::allocator<class tpRigidBody * const> > * tpRigidBody::rigidBodyList" (?rigidBodyList@tpRigidBody@@2PAV?$vector@QAVtpRigidBody@@std@@A) tpRigidBody.obj


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    A static variable is one which must be initialised before the program begins; and this initialisation must be carried out in what's called file scope.

    Effectively this means changing your implementation file to the following;

    [PHP]
    #include "tpRigidBody.h"

    vector<tpRigidBody * const> *tpRigidBody::rigidBodyList = new vector<tpRigidBody * const>();

    tpRigidBody::tpRigidBody(){
    rigidBodyList->push_back(this);
    }

    tpRigidBody::~tpRigidBody(){

    }
    [/PHP]


  • Closed Accounts Posts: 2,103 ✭✭✭Tiddlypeeps


    Oh ye, of course. Thanks a mill.


Advertisement