Connecting Tech Pros Worldwide Help | Site Map

Reference Errors with vectors

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 26th, 2006, 04:05 PM
Felix85
Guest
 
Posts: n/a
Default Reference Errors with vectors

here is my 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);
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
while(pos2 <= str.length()){
pos2 = str.find_first_of(delimiter, pos1);
if(pos2 > str.length()){
pos2 = str.length();
}
words.push_back(str.substr(pos1 ,pos2 - pos1));
pos1 = pos2 + 1;
pos2 += 1;
}
}
static void getCommand(){
string c;
cin >> c;
tokenizer(c, com, " ");
if(com.size() == 1){
cout << com[1] << "\n";
} else if(com.size() == 2){
cout << com[1] << "\n" << com[2] << "\n";
} else { cout << "invalid command!\n"; }
}
private:
static vector<string> com;
};

int main()
{
command::getCommand();
return 0;
}



Here is the error I get when I compile:

/tmp/ccNSmcoY.o: In function `command::getCommand()':
command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x70): undefined
reference to `command::com'
command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0xeb): undefined
reference to `command::com'
command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x104):
undefined reference to `command::com'
command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x135):
undefined reference to `command::com'
command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x14e):
undefined reference to `command::com'
/tmp/ccNSmcoY.o:command.cpp:(.gnu.linkonce.t._ZN7comman d10getCommandEv+0x165):
more undefined references to `command::com' follow
collect2: ld returned 1 exit status




If anyone can find something please let me know. thanks in advance.


  #2  
Old May 26th, 2006, 04:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Reference Errors with vectors

Felix85 wrote:[color=blue]
> here is my 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);
> words.push_back(str.substr(pos1 ,pos2 - pos1));
> pos1 = pos2 + 1;
> while(pos2 <= str.length()){
> pos2 = str.find_first_of(delimiter, pos1);
> if(pos2 > str.length()){
> pos2 = str.length();
> }
> words.push_back(str.substr(pos1 ,pos2 - pos1));
> pos1 = pos2 + 1;
> pos2 += 1;
> }
> }
> static void getCommand(){
> string c;
> cin >> c;
> tokenizer(c, com, " ");
> if(com.size() == 1){
> cout << com[1] << "\n";
> } else if(com.size() == 2){
> cout << com[1] << "\n" << com[2] << "\n";
> } else { cout << "invalid command!\n"; }
> }
> private:
> static vector<string> com;[/color]

Static data members have to be _defined_ at the namespace level.
Isn't this already in the FAQ?
[color=blue]
> };[/color]

Put here:

vector<string> command::com;
[color=blue]
>
> int main()
> {
> command::getCommand();
> return 0;
> }
>
>
>
> Here is the error I get when I compile:
>
> /tmp/ccNSmcoY.o: In function `command::getCommand()':
> command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x70):
> undefined reference to `command::com'
> command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0xeb):
> undefined reference to `command::com'
> command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x104):
> undefined reference to `command::com'
> command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x135):
> undefined reference to `command::com'
> command.cpp:(.gnu.linkonce.t._ZN7command10getComma ndEv+0x14e):
> undefined reference to `command::com'
> /tmp/ccNSmcoY.o:command.cpp:(.gnu.linkonce.t._ZN7comman d10getCommandEv+0x165):
> more undefined references to `command::com' follow
> collect2: ld returned 1 exit status
>
>
>
>
> If anyone can find something please let me know. thanks in advance.[/color]

Please read the FAQ before posting.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #3  
Old May 26th, 2006, 04:15 PM
Felix85
Guest
 
Posts: n/a
Default Re: Reference Errors with vectors

that doesn't work, ive already tried that. It just gives me the same
error.

  #4  
Old May 26th, 2006, 04:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Reference Errors with vectors

Felix85 wrote:[color=blue]
> that doesn't work, ive already tried that. It just gives me the same
> error.[/color]

What are you talking about? What doesn't work?


  #5  
Old May 26th, 2006, 04:25 PM
Felix85
Guest
 
Posts: n/a
Default Re: Reference Errors with vectors

I still get the same error after trying what you said to do. Unless im
not understanding what you are trying to have me do.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.