0

I am getting an error saying this:

error: no matching function for call to 'Expression::shuntingYard(Expression&)'

while the function is declared in the header folder named Expression.h. I can't see what the problem is. I also included all the necessary pre-processor directives.

The following files are included:

main.cpp

#include <iostream>
#include "Expression.h"

using namespace std;

int main()
{
    Expression expr("2 * (3 + 1)");

    //Set x = 5
    //expr.instantiateVariable('x',5);
    //Set y = 3
    //expr.instantiateVariable('y',3);

    cout << "Answer: " << expr.shuntingYard(expr) << endl;
}

Expression.h

#ifndef EXPRESSION_H
#define EXPRESSION_H

#include <string>
#include <iostream>

using namespace std;

class Expression
{
    private:
        string expression;
    public:
        Expression(string expr);
        ~Expression();
        void instantiateVariable(char name, int value);

        //Function to calculate the postFix string made by the ShuntingYard function
        int evaluate(string, int, int);

        //Function to convert infix expression to postfix
        string shuntingYard(string);

        //Other
        int higherPrecedence(char operator1, char operator2);
        bool IsOperator(char C);
        bool IsOperand(char C);
};

#endif

If you can please point out to what I am doing wrong, by receiving this error, I will appreciate it.

Thank you.

1
  • So you have no such function signature, and you don't provide any automatic conversion from Expression to std::string either. Commented Aug 4, 2014 at 19:23

2 Answers 2

3

The declared function shuntingYard takes a string by value, not an Expression by reference.

As a side-note, adding operator string() to class Expression would solve your problem:

operator string() const
{
    return expression;
}

SUPPLAMENTAL

This is also an option (one which I personally prefer):

operator string&() const
{
    return expression;
}

In this case, removing the const will allow you to change the expression member variable outside.

Sign up to request clarification or add additional context in comments.

Comments

0

In the class definition you declared function

string shuntingYard(string);

having a parameter of type std::string

However in main you call function with the same name but passing an argument of type Expression

cout << "Answer: " << expr.shuntingYard(expr) << endl;

And there is no conversion function of object of type Expression to an object of type std::string in the class definition that could be called implicitly.

So the compiler does not see the declaration of the called function and issues the error.

It is the result of the incorrect class design. Function shuntingYard have to use data member string expression; instead of a string used as argument. That is the function should be declared without the parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.