Z
zl2k
I searched the topic and noticed that the initilization of static needs
to be treated specially, but not know how. Here is my code which giving
the link error:
test.h
-------------------------------------
#ifndef TEST_H
#define TEST_H
#include <vector>
#include <iostream>
using namespace std;
class Test{
public:
static vector<int> v;
Test();
~Test();
};
#endif
---------------------------------------
test.cpp
--------------------------------------
#include <test.h>
Test::Test() {
v.push_back(13);
cout<<v.back();
}
int main(){
Test t;
exit(1);
}
---------------------------------------
error message:
--------------------------------------
Building CXX object CMakeFiles/test.dir/test.o
Linking CXX executable test
CMakeFiles/test.dir/test.o(.text+0x111): In function `Test::Test()':
: undefined reference to `Test::v'
CMakeFiles/test.dir/test.o(.text+0x124): In function `Test::Test()':
: undefined reference to `Test::v'
CMakeFiles/test.dir/test.o(.text+0x157): In function `Test::Test()':
: undefined reference to `Test::v'
CMakeFiles/test.dir/test.o(.text+0x16a): In function `Test::Test()':
: undefined reference to `Test::v'
----------------------------------------
If the v is not static, then no error. How may I use the static vector?
Thanks ahead.
zl2k
to be treated specially, but not know how. Here is my code which giving
the link error:
test.h
-------------------------------------
#ifndef TEST_H
#define TEST_H
#include <vector>
#include <iostream>
using namespace std;
class Test{
public:
static vector<int> v;
Test();
~Test();
};
#endif
---------------------------------------
test.cpp
--------------------------------------
#include <test.h>
Test::Test() {
v.push_back(13);
cout<<v.back();
}
int main(){
Test t;
exit(1);
}
---------------------------------------
error message:
--------------------------------------
Building CXX object CMakeFiles/test.dir/test.o
Linking CXX executable test
CMakeFiles/test.dir/test.o(.text+0x111): In function `Test::Test()':
: undefined reference to `Test::v'
CMakeFiles/test.dir/test.o(.text+0x124): In function `Test::Test()':
: undefined reference to `Test::v'
CMakeFiles/test.dir/test.o(.text+0x157): In function `Test::Test()':
: undefined reference to `Test::v'
CMakeFiles/test.dir/test.o(.text+0x16a): In function `Test::Test()':
: undefined reference to `Test::v'
----------------------------------------
If the v is not static, then no error. How may I use the static vector?
Thanks ahead.
zl2k