Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Linker Error with static vector

  • 12-04-2010 02: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, Registered Users 2 Posts: 1,919 ✭✭✭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