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.
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.
// 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
}
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?
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);
}
}
}
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!