Undefined reference to 'get_token()'

Hi, i have just started to learn programming for about a week and i started off using the book Principle and Practice C++. The book moved rather fast and i am on the chapter of Token now.I followed the book's example but to find myself getting undefined reference to 'get_token()' on my compiler. I do not know which part of my code went wrong as i am already copying word for word from the book already.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "std_lib_facilities.h"

class Token {
public:
        char kind;
        double value;
        Token(char ch)
                :kind(ch), value(0) {}
        Token(char ch, double val)
                :kind(ch), value(val) {}
};

Token get_token();
vector<Token> tok;

int main()
{
while(cin){
    Token t = get_token();
    tok.push_back(t);
}
for (int i=0; i<tok.size(); ++i){
    if (tok[i].kind=='*'){
            double d=tok[i-1].value*tok[i+1].value;
            }
        }

    return 0;
}


The book says that this example of code "can read out input into a vector of Tokens".

Can someone please tell me why my compiler doesn't let me compile? thank you!
EDIT: the error from my compiler says its on line 19
Last edited on
closed account (z05DSL3A)
Line 13 declares the function get_token() but there is no definition for the function in the code.
Hi noobgrammer,

you haven't defined get_token(), you have declared it, but not defined it. So you get an error at link time when you try to assign a Token to the return value of get_token() on line 19.

Last edited on
Hi Grey Wolf and dangrr888. thank you very much for replying. May i also ask how should i define get_token() ?

when we say define, does it mean to write something in the bracket "( )" or does it mean to write something in the curly bracket {.. return .. } ?

and, what are the something to write ?

thank you very much once again.
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
// declaration of get_token() [A.K.A. prototype of get_token() ]
Token get_token();

// definition of get_token()
Token get_token()
{
   // Some code here that returns a Token
}


what are the something to write ?

Is it not in the book example?
ok. if i did not google wrongly, definitions are written in the curly brackets yea? but how should get_token() be defined? and the purpose of get_token(), (if i didn't understand wrongly) is to cin an input?
Hi noobgrammer,

definitions are written in the curly brackets yea?

that's right, see what Grey Wolf did above.

but how should get_token() be defined? and the purpose of get_token(),

thats completely up to you! A basic definition would be for get_token() to return an empty Token:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "std_lib_facilities.h"

class Token {
public:
        Token(char ch = char()) //default initialised kind so that we can invoke default constructor (i.e. with no arguments)
                :kind(ch), value(0) {}
        Token(char ch, double val)
                :kind(ch), value(val) {}

        char kind;
        double value;
};

Token get_token() {return Token();} //returns an empty Token
vector<Token> tok;

int main()
{
while(cin){
    Token t = get_token();
    tok.push_back(t);
}
for (int i=0; i<tok.size(); ++i){
    if (tok[i].kind=='*'){
            double d=tok[i-1].value*tok[i+1].value;
            }
        }

    return 0;
}


is to cin an input?

Yes, cin is an istream object and is also the default input stream for clients to enter data (i.e., the keyboard).
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Token get_token()
{
    // Assuming you are doing some sort of calculator from the code in main()
    // Something like this...
    char ch;
    cin >> ch;

    switch (ch)
    {
        case '*':
           return Token(ch);
        case '0': case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9': case '.': 
            {
                cin.putback(ch);
                double value;
                cin >> value;
                return Token('9',value); //Not sure about the char, using 9 to indicate a numeral value
            }
        default:
            { // at this point only numbers and '*' are valid error handeling would need to be improved.
                cout << "Error: Invalid Token << endl;
                exit(1);
            }
    }
} 
NB:Un-tested, un-compiled code
Last edited on
oh...... so this is how defining is done! thank you very much Grey Wolf! i googled till my hair turns white but still couldn't find a definition that allows get_token() to cin an input.
thank you very much!

and, thank you dangrr888! i am now clearer between declaration and definition and the purpose of get_token. thank you!
Your welcome, glad to help!
Topic archived. No new replies allowed.