The OP asked this:
I know that i can just create an Empty constructor, but why this error happens?
Your class NumberExpression
has a constructor that takes a Token
object.
In this class's constructor body you are using Token's
copy assignment operator. m_token = tok;
that is defined by the compiler by default.
In your class Token
the constructor that you declared & defined is expecting 2 parameters in its parameter list. Yet when you try to use the copy assignment operator this is why the compiler is giving you that error. You are trying to assign a copy of a Token
object and save(store) it to a member of NumberExpression
. Since you declared a constructor
for Token
that takes two parameters the compiler will not use the default ctor
or copy ctor
because you have not supplied one after declaring a user defined constructor
and it can not find the declaration or definition of a default ctor
.
There are three ways to fix this:
Provide a Default or empty constructor in the header simply as:
Token() = default;
Token();
Token() {}
Use NumberExpression's
class member initializer list as others have stated:
NumberExpression::NumberExpression( Token token ) : m_token( token ) {...}
Or use both in combination.
Also since you are passing a single object into NumberExpression's
constructor
if you want to prevent automatic implicit conversion or casting by the compiler and want to make it strict you can use the explicit
keyword on the Constructor's declaration:
class NumberExpression : public Expression {
private:
Token m_token;
public:
explicit NumberExpression( Token token ) : m_token( token ) {}
double evaluate();
};
To set the default initialization list for the token class:
class Token {
public:
enum Type {
INVALID = -1,
NUM,
ADD,
SUB,
MUL,
DIV,
EOF
};
private:
char* m_text;
Type m_type;
public:
Token(Type type, char* text) : m_type(INVALID), m_text(nullptr) {}
Type getType() const { return m_type; }
char* getText() const { return m_text; }
};