undefined reference

F

Felix85

Here is the error i am getting:

/tmp/ccu9LP0x.o: In function
`command::tokenizer(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)':
command.cpp:(.gnu.linkonce.t._ZN7command9tokenizerESsSt6vectorISsSaISsEESs+0x59):
undefined reference to `command::com'
command.cpp:(.gnu.linkonce.t._ZN7command9tokenizerESsSt6vectorISsSaISsEESs+0xe9):
undefined reference to `command::com'
collect2: ld returned 1 exit status


Here is the source code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class command {
public:
static void tokenizer(string str, vector<string> words, string
delimiter){
string::size_type pos1 = 0;
string::size_type pos2 = str.find_first_of(delimiter);
com.push_back(str.substr(pos1 ,pos2 - 1));
pos1 = pos2 + 1;
while(pos2 != string::npos){
pos2 = str.find_first_of(delimiter, pos2);
com.push_back(str.substr(pos1 ,pos2 - 1));
pos1 = pos2 + 1;
}
}
static void getCommand(string c){
tokenizer(c, com, " ");
if(com.size() == 1){
} else if(com.size() == 2){
} else { }
}
private:
static vector<string> com;
};

int main()
{
vector<string> test;
string t = "hello how are you doing today";
command::tokenizer(t, test, " ");
cout << "there are " << test.size() << " words in that string.\n";
int i;
for(i = 0; i < test.size(); i++){
cout << test;
}
return 0;
}



Thanks in advance.
 
N

Noah Roberts

Felix85 wrote:
command.cpp:(.gnu.linkonce.t._ZN7command9tokenizerESsSt6vectorISsSaISsEESs+0xe9):
undefined reference to `command::com'


Here is the source code:
class command {
public:
static void tokenizer(string str, vector<string> words, string
delimiter){
string::size_type pos1 = 0;
string::size_type pos2 = str.find_first_of(delimiter);
com.push_back(str.substr(pos1 ,pos2 - 1));

That is your first use of this variable and I don't see it defined
anywhere either.
 
J

Jonathan Mcdougall

Felix85 said:
Here is the error i am getting:

class command {

private:
static vector<string> com;
};

static member objects must be defined somewhere. In your implementation
file (.cpp), add

vector<string> command::com;

Note that this is a definition, so you could use another constructor,
such as

// a vector with 20 default-constructed elements
vector<string> command::com(20);


Jonathan
 
M

Marcus Kwok

Felix85 said:
Here is the error i am getting:

/tmp/ccu9LP0x.o: In function
`command::tokenizer(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)':
command.cpp:(.gnu.linkonce.t._ZN7command9tokenizerESsSt6vectorISsSaISsEESs+0x59):
undefined reference to `command::com'
command.cpp:(.gnu.linkonce.t._ZN7command9tokenizerESsSt6vectorISsSaISsEESs+0xe9):
undefined reference to `command::com'
collect2: ld returned 1 exit status


Here is the source code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class command {
public:
static void tokenizer(string str, vector<string> words, string
delimiter){
string::size_type pos1 = 0;
string::size_type pos2 = str.find_first_of(delimiter);
com.push_back(str.substr(pos1 ,pos2 - 1));
pos1 = pos2 + 1;
while(pos2 != string::npos){
pos2 = str.find_first_of(delimiter, pos2);
com.push_back(str.substr(pos1 ,pos2 - 1));
pos1 = pos2 + 1;
}
}
static void getCommand(string c){
tokenizer(c, com, " ");
if(com.size() == 1){
} else if(com.size() == 2){
} else { }
}
private:
static vector<string> com;

Here, you have declared com, but you still also need a definition.

Outside of the class, you need:

vector<string> command::com;

However, after adding this, it seems your tokenizer() function is stuck
in an infinite loop.
};

int main()
{
vector<string> test;
string t = "hello how are you doing today";
command::tokenizer(t, test, " ");
cout << "there are " << test.size() << " words in that string.\n";
int i;
for(i = 0; i < test.size(); i++){
cout << test;
}
return 0;
}



Thanks in advance.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top