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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Linker Error with static vector

  • 12-04-2010 1: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,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