0

I am trying to solve the first exercise of Stroustrup's book about C++ programming principles. I'm pretty stuck with two issues.

The first is that he says that there are three Compile-Time errors but I can see just one and, my second answer, is to ask to you if you can explain me better what kind of issue it is.

    struct Token {
    char kind;
    double value;
    string name;
    Token(char ch) :kind(ch), value(0) { }
    Token(char ch, double val) :kind(ch), value(val) { }
};

class Token_stream {
    bool full;
    Token buffer;
public:
    Token_stream() :full(0), buffer(0) { }

    Token get();
    void unget(Token t) { buffer = t; full = true; }

    void ignore(char);
};

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';

Token Token_stream::get()
{
    if (full) { full = false; return buffer; }
    char ch;
    cin >> ch;
    switch (ch) {
    case '(':
    case ')':
    case '+':
    case '-':
    case '*':
    case '/':
    case '%':
    case ';':
    case '=':
        return Token(ch);
    case '.':
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    {   cin.unget();
    double val;
    cin >> val;
    return Token(number, val);
    }
    default:
        if (isalpha(ch)) {
            string s;
            s += ch;
            while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) s = ch;
            cin.unget();
            if (s == "let") return Token(let);
            if (s == "quit") return Token(name);
            return Token(name, s);
        }
        error("Bad token");
    }
}

void Token_stream::ignore(char c)
{
    if (full && c == buffer.kind) {
        full = false;
        return;
    }
    full = false;

    char ch;
    while (cin >> ch)
        if (ch == c) return;
}

struct Variable {
    string name;
    double value;
    Variable(string n, double v) :name(n), value(v) { }
};

He created a struct of a token, then he wrote a struct for a variable too that stores a name and a corresponding value. I can't figure out how to solve the compile-time error about return Token(name,s);. It says (correctly) that it is impossible to convert a const char name in string. I tried to work on and convert it declaring as string but it didn't work.

Can you help me please?

Thank you very much!

Leo

2 Answers2

1

Lets focus then on the line:

return Token(name, s);

The compile time error I get is:

>  Source.cpp
source.cpp(72): error C2665: 'Token::Token' : none of the 3 overloads could     convert all the argument types
>          source.cpp(11): could be 'Token::Token(char,double)'
>          while trying to match the argument list '(const char, std::string)'

For Token(name, s), name is a const char and s is a std::string.

ie we are calling Token(const char, std::string)

Do we have a possible candidate for this?

Well, we have these two Token constructors:

Token(char ch)
Token(char ch, double val)

Do they match Token(const char, std::string)?

The answer is no.

So you either have to create a Token constructor which does match or you change the line to match the two ctors you have.

Angus Comber
  • 9,316
  • 14
  • 59
  • 107
  • Hi Angus! Thanks for answering. I did, in fact, create a new constructor `Token(string na, double val) :name(na),value(val) { }` but it is still continuing to give me that error! This kind of construction has been done by the programmer inside `struct Variable` that is defined after the warning line! Then I thought it was redundant to construct, again and in a different struct, essentially the same thing! :| Furthermore it still does not compile :@ – Leonardo Urbano Jul 19 '15 at 11:37
  • You need to create Token (const char ch, std::string& s) – Angus Comber Jul 19 '15 at 13:39
0

To remove the compile error:

Add:

Token(char ch, string s): kind(ch), name(s){}

Full code:

struct Token
{
    char kind;
    double value;
    string name;
    Token(char ch) : kind(ch), value(0) {}
    Token(char ch, string s): kind(ch), name(s){}
    Token(char ch, double val) : kind(ch), value(val) {}
};
Nong Tinh
  • 1
  • 1