stupid question

F

Flzw

I have a class declared in a header file that has a static vector variable
which I get unresolved external errors when I try to access it, I understand
static members should be declared in cpp but how is it done in this case?
Couldn't find anything about that, hope I am clear enough, thanks for any
help.
 
M

Martin Magnusson

Flzw said:
I have a class declared in a header file that has a static vector variable
which I get unresolved external errors when I try to access it, I understand
static members should be declared in cpp but how is it done in this case?

Like this:

// In X.hpp:
class X
{
static std::vector<int> v;
};

// In X.cpp:
std::vector<int> X::v;

/ martin
 
D

Dietmar Kuehl

Flzw said:
I have a class declared in a header file that has a static vector variable
which I get unresolved external errors when I try to access it, I understand
static members should be declared in cpp but how is it done in this
case?

You *declared* the static member in your class. You have apparently
nowhere *defined* the member:

| // file: foo.hpp
| struct foo {
| static std::vector<int> bar; // declaration
| // ...
| };

| // file: foo.cpp
| #include "foo.hpp"
| std::vector<int> foo::bar; // definition

Of course, the header should be protected against mulitple inclusion
but this should highlight the needed definition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top