Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > undefined reference

Reply
Thread Tools

undefined reference

 
 
Felix85
Guest
Posts: n/a
 
      05-10-2006
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._ZN7command9tokenizer ESsSt6vectorISsSaISsEESs+0x59):
undefined reference to `command::com'
command.cpp.gnu.linkonce.t._ZN7command9tokenizer ESsSt6vectorISsSaISsEESs+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[i];
}
return 0;
}



Thanks in advance.

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      05-10-2006

Felix85 wrote:
command.cpp.gnu.linkonce.t._ZN7command9tokenizer ESsSt6vectorISsSaISsEESs+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.

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-10-2006
Felix85 wrote:
> Here is the error i am getting:
>
> class command {


<snip>

> 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

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      05-10-2006
Felix85 <(E-Mail Removed)> wrote:
> 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._ZN7command9tokenizer ESsSt6vectorISsSaISsEESs+0x59):
> undefined reference to `command::com'
> command.cpp.gnu.linkonce.t._ZN7command9tokenizer ESsSt6vectorISsSaISsEESs+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[i];
> }
> return 0;
> }
>
>
>
> Thanks in advance.
>


--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
amit.limaye@gmail.com
Guest
Posts: n/a
 
      05-10-2006
try command::com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
'Undefined' Client-Side Object Reference Felipe ASP .Net 3 07-16-2004 04:01 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
Error: 'undefined reference' in g++ but gcc succeeded Lu C++ 1 07-10-2003 12:55 PM



Advertisments