undefined reference

Discussion in 'C++' started by Felix85, May 10, 2006.

  1. Felix85

    Felix85 Guest

    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.
    Felix85, May 10, 2006
    #1
    1. Advertising

  2. Felix85

    Noah Roberts Guest

    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.
    Noah Roberts, May 10, 2006
    #2
    1. Advertising

  3. 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
    Jonathan Mcdougall, May 10, 2006
    #3
  4. Felix85

    Marcus Kwok Guest

    Felix85 <> 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._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.
    >


    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, May 10, 2006
    #4
  5. Felix85

    Guest

    try command::com
    , May 10, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Felipe
    Replies:
    3
    Views:
    437
    Felipe
    Jul 16, 2004
  2. Lu
    Replies:
    1
    Views:
    559
    Roel Schroeven
    Jul 10, 2003
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,723
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    573
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    356
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page