samedi 8 mai 2021

C++ why am I getting this strange error for no matching function call

I have the following code segment:

UnaryExpression.h:

#ifndef UNARYEXPRESSION_H_
#define UNARYEXPRESSION_H_

#include "Expression.h"

class UnaryExpression : public Expression {
private:
    Token op;
    Expression exp;
protected:
    std::map<std::string, float> scope;
public:
    UnaryExpression(std::map<std::string, float> &scope, Token& op, Expression& exp);
    virtual ~UnaryExpression();

    void evaluate();
    float getReturnValue();
};

#endif /* UNARYEXPRESSION_H_ */

UnaryExpression.cpp:

#include "UnaryExpression.h"

UnaryExpression::UnaryExpression(std::map<std::string, float> &scope, Token& op, Expression& exp) : Expression(scope) {
    this->op = op;
    this->exp = exp;
}

UnaryExpression::~UnaryExpression() {}

void UnaryExpression::evaluate() {}

float UnaryExpression::getReturnValue() {
    return this->returnResult;
}

I am constantly getting an error, but as soon as I remove Expression exp; from the constructor and private variables, the error seems to dissapear and I'm not sure why this is happening:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\UnaryExpression.o" "..\\src\\UnaryExpression.cpp" 

..\src\UnaryExpression.cpp: In constructor 'UnaryExpression::UnaryExpression(std::map<std::__cxx11::basic_string<char>, float>&, Token&, Expression&)':
..\src\UnaryExpression.cpp:10:117: error: no matching function for call to 'Expression::Expression()'
   10 | UnaryExpression::UnaryExpression(std::map<std::string, float> &scope, Token& op, Expression& exp) : Expression(scope) {
      |                                                                                                                     ^
In file included from ..\src\UnaryExpression.h:11,
                 from ..\src\UnaryExpression.cpp:8:
..\src\Expression.h:18:2: note: candidate: 'Expression::Expression(std::map<std::__cxx11::basic_string<char>, float>&)'
   18 |  Expression(std::map<std::string, float> &scope);
      |  ^~~~~~~~~~
..\src\Expression.h:18:2: note:   candidate expects 1 argument, 0 provided
..\src\Expression.h:13:7: note: candidate: 'Expression::Expression(const Expression&)'
   13 | class Expression : public Statement {
      |       ^~~~~~~~~~
..\src\Expression.h:13:7: note:   candidate expects 1 argument, 0 provided

Build Failed. 1 errors, 0 warnings. (took 1s.89ms)

For reference, here is the definition of Expression.h:

#ifndef EXPRESSION_H_
#define EXPRESSION_H_

#include "Statement.h"

class Expression : public Statement {
protected:
    std::map<std::string, float> scope;
    float returnResult;
public:
    Expression(std::map<std::string, float> &scope);
    virtual ~Expression();

    virtual void evaluate();
    virtual float getReturnValue();
};

#endif /* EXPRESSION_H_ */

The class Statement is similar to Expression, it has only a virtual method void evaluate() and a scope. When I initialize Expression, of course I pass the scope to the parent Statement class.

Aucun commentaire:

Enregistrer un commentaire