473,473 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 23561
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
2
by: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
2
by: zl2k | last post by:
hi, all Suppose I have the following header file: #include <vector> using namespace std; class Test{ public: ~Test(); Test();
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
3
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other....
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.