 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Serge Guest
|
Posted: Sat Dec 18, 2004 3:01 pm Post subject: unresolved external symbol linker error with a vector which |
|
|
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);
}
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Sat Dec 18, 2004 6:24 pm Post subject: Re: unresolved external symbol linker error with a vector wh |
|
|
On Sat, 18 Dec 2004 16:01:54 +0100 in comp.lang.c++, "Serge"
<serge (AT) NOSPAM (DOT) com> wrote,
| Quote: | //define static member variables
vector<int> m_variable_v;
|
Should be:
vector<int> myclass::m_variable_v;
|
|
| Back to top |
|
 |
Serge Guest
|
Posted: Sat Dec 18, 2004 8:34 pm Post subject: Re: unresolved external symbol linker error with a vector wh |
|
|
Great ! Thanks very much for your quick solution.
Best regards,
Serge
"David Harmon" <source (AT) netcom (DOT) com> wrote
| Quote: | On Sat, 18 Dec 2004 16:01:54 +0100 in comp.lang.c++, "Serge"
[email]serge (AT) NOSPAM (DOT) com[/email]> wrote,
//define static member variables
vector<int> m_variable_v;
Should be:
vector<int> myclass::m_variable_v;
|
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sat Dec 18, 2004 10:56 pm Post subject: Re: unresolved external symbol linker error with a vector wh |
|
|
"Serge" <serge (AT) NOSPAM (DOT) com> wrote
| Quote: | 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
|
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.
| Quote: | };
//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;
| Quote: | 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.
| Quote: | {
m_variable_v.push_back(new_value);
}
|
-Mike
|
|
| Back to top |
|
 |
Paavo Helde Guest
|
Posted: Sun Dec 19, 2004 5:39 pm Post subject: Re: unresolved external symbol linker error with a vector wh |
|
|
"Serge" <serge (AT) NOSPAM (DOT) com> wrote in
news:41c44669$0$13470$ba620e4c (AT) news (DOT) skynet.be:
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|