Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unresolved external symbol linker error with a vector which is a static member variable

Reply
Thread Tools

unresolved external symbol linker error with a vector which is a static member variable

 
 
Serge
Guest
Posts: n/a
 
      12-18-2004
Hi,
I have no problem creating a static member variable with integers, etc but
when I try the same with a vector then I always get linker errors that the
static member variable is unknown (unresolved external symbol)
Below is what an example of the code I use. Can somebody tell me what I am
doing wrong ?
Thanks very much in advance,
Serge

//myclass.h
//header file
#include <vector>

using namespace std;

class myclass
{
public:
static void my_method (int new_value);

private:
static vector<int> m_variable_v; //static member variable
};



//myclass.cpp
//cource code class

#include "myclass.h"

using namespace std;

//define static member variables
vector<int> m_variable_v;


void myclass::my_method(int new_value)
{
m_variable_v.push_back(new_value);
}


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      12-18-2004
On Sat, 18 Dec 2004 16:01:54 +0100 in comp.lang.c++, "Serge"
<(E-Mail Removed)> wrote,
>//define static member variables
>vector<int> m_variable_v;


Should be:
vector<int> myclass::m_variable_v;

 
Reply With Quote
 
 
 
 
Serge
Guest
Posts: n/a
 
      12-18-2004
Great ! Thanks very much for your quick solution.
Best regards,
Serge

"David Harmon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sat, 18 Dec 2004 16:01:54 +0100 in comp.lang.c++, "Serge"
> <(E-Mail Removed)> wrote,
> >//define static member variables
> >vector<int> m_variable_v;

>
> Should be:
> vector<int> myclass::m_variable_v;
>



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-18-2004

"Serge" <(E-Mail Removed)> wrote in message
news:41c44669$0$13470$(E-Mail Removed)...
> Hi,
> I have no problem creating a static member variable with integers, etc but
> when I try the same with a vector then I always get linker errors that the
> static member variable is unknown (unresolved external symbol)
> Below is what an example of the code I use. Can somebody tell me what I am
> doing wrong ?
> Thanks very much in advance,
> Serge
>
> //myclass.h
> //header file
> #include <vector>
>
> using namespace std;
>
> class myclass
> {
> public:
> static void my_method (int new_value);
>
> private:
> static vector<int> m_variable_v; //static member variable


This is a declaration, not a definition. No actual object has
been created. And still no vector object will be created
if you create a type 'myclass' object, because 'static' means
that the member is not stored as part of a 'myclass' object,
but separately, only once. I.e. 'm_variable_v's scope is
your class, but it is not actually part of a 'myclass' object.

You need to provide (exactly one)
definition of your static vector.

> };
>
>
>
> //myclass.cpp
> //cource code class
>
> #include "myclass.h"
>
> using namespace std;
>
> //define static member variables
> vector<int> m_variable_v;


Not quite right. This defines a vector which is *not*
in your class' scope. It has nothing to do with your
class or any objects of that type.

vector<int> myclass::m_variable_v;

> void myclass::my_method(int new_value)


See how you've qualified 'my_method' as being a member
of 'myclass' here? Same is required for static data members.

> {
> m_variable_v.push_back(new_value);
> }



-Mike


 
Reply With Quote
 
Paavo Helde
Guest
Posts: n/a
 
      12-19-2004
"Serge" <(E-Mail Removed)> wrote in
news:41c44669$0$13470$(E-Mail Removed):

> Hi,
> I have no problem creating a static member variable with integers, etc
> but when I try the same with a vector then I always get linker errors
> that the static member variable is unknown (unresolved external

[...]
> //define static member variables
> vector<int> m_variable_v;


vector<int> myclass::m_variable_v;

HTH
Paavo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is Error 1 error LNK2019: unresolved external symbol ???? Trent C++ 9 06-29-2007 08:32 PM
Linker complains about unresolved symbol in STL mapp class Vijay Bajwa C++ 3 01-26-2007 07:08 PM
Using Py_InitModule3 -> [Linker Error] Unresolved external '_Py_InitModule4TraceRefs' iwl Python 2 12-09-2006 01:07 PM
[Linker Error] Unresolved external Laszlo C Programming 1 12-06-2004 09:29 AM
unresolved external symbol/using an external dll Scott Allen C++ 8 05-02-2004 06:11 PM



Advertisments