469,904 Members | 2,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

how to initialize a static vector

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

May 20 '06 #1
6 23112
I think it should be #include "test.h" in the test.cpp

From
Visal .In

zl2k wrote:
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


May 20 '06 #2
On Sat, 20 May 2006 08:34:49 -0700, zl2k wrote:
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: If the v is not static, then no error. How may I use the static vector?
Thanks ahead.


go back to your copy of the c++ manual and look for "definition vs.
declaration". you did one but not the other. both are required.
May 20 '06 #3
zl2k wrote:
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:
The following line states that 'Test::v' exists somewhere...
static vector<int> v;
Test();
~Test();
};
#endif
---------------------------------------

test.cpp
--------------------------------------
For non-system headers use the quoted form, like this:

#include "test.h"
#include <test.h>

The following line actually creates Test::v

vector<int> Test::v;

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

May 20 '06 #4
zl2k wrote:
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;
Never in a header,
http://www.parashift.com/c++-faq-lit....html#faq-27.5
for other cases.
class Test{
public:
static vector<int> v;
This is a declaration, not a definition. You must define that object
somewhere (and only once). Usually, you'll want to define it in the
source file that goes with this header.
Test();
~Test();
};
#endif
---------------------------------------

test.cpp
--------------------------------------
#include <test.h>
How a file specified in an include directive is searched for is
implementation-defined. However, most compilers assume <file>s are
standard or system headers and "file"s are user-defined headers. That
means both #include <test.h> and #include "iostream" have good chances
to fail.

While we're here, just define the static member object here.

vector<int> Test::v;

Since this is an definition, you can also initialize it:

vector<int> Test::v(10); // vector with 10 elements
Test::Test() {
v.push_back(13);
cout<<v.back();
}

int main(){
Test t;
exit(1);
You don't need to call exit() here. Just return 0 or nothing.
}
---------------------------------------

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'

Jonathan

May 21 '06 #5
Thanks for all the comments, they are very helpful.

zl2k

May 21 '06 #6
* Jonathan Mcdougall:
How a file specified in an include directive is searched for is
implementation-defined. However, most compilers assume <file>s are
standard or system headers and "file"s are user-defined headers. That
means both #include <test.h> and #include "iostream" have good chances
to fail.


The first, yes; the second, perhaps in practice, although I haven't
tried so cannot say.

However, the standard specifies that "f" includes the search of <f>,
after a possible implementation-specified search.

In practice, with most compilers, "f" searches in the directory of the
including file first, and then performs an <f> search.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by cppaddict | last post: by
2 posts views Thread by slack_justyb | last post: by
5 posts views Thread by Jim Langston | last post: by
18 posts views Thread by toton | last post: by
6 posts views Thread by Jia | last post: by
9 posts views Thread by Steven Woody | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.