how to initialize a static vector

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
 
N

noone

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.
 
L

Larry I Smith

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

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

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

The following line actually creates Test::v
 
J

Jonathan Mcdougall

zl2k said:
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-lite/coding-standards.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

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 said:
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
 
A

Alf P. Steinbach

* 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.
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top