Question

I'm currently writing a basic program to evaluate mathematical expressions which I will then later use in genetic programming to determine the best solution to a system of expressions. My compiler keeps complaining but I'm almost sure that I've got everything right.

The error:

C:\Users\Baelic Edeyn\Desktop\Project\Equation\Shunting yard\Better>make  
g++ -g -c Shunt.cpp  
g++ -g -c main.cpp  
main.cpp: In function 'int main()':  
main.cpp:18:19: error: 'shuntingYard' was not declared in this scope  
make: *** [main.o] Error 1

My Makefile:

main: Shunt.o main.o  
    g++ -g Shunt.o main.o -o main  
main.o:main.cpp  
    g++ -g -c main.cpp  
Shunt.o:Shunt.cpp  
    g++ -g -c Shunt.cpp

My main:

#include "Shunt.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string expr = "3+ 47 *2/(1-5)^2^3";
    string expr1 = "a+b";
    string expr2 = "(a+b)";
    string expr3 = "a+b*!3";
    string expr4 = "(a+b)*!3";

    cout << "expression" << "   " << "postfix" << endl;
    cout << expr << "   ";
    shuntingYard(expr);
    cout << expr << endl;
    cout << expr1 << "      ";
    ...
    return 0;

}

My header file:

#ifndef SHUNT_H
#define SHUNT_H

#include <string>

using namespace std;

class Shunt
{
    public:
        int precedence(char);
        void shuntingYard(string &expr);
        bool isFunction(string);
        bool isOperator(char);
        bool isLeftAssociative(char);
        bool isNum(char);

    private:    

};

#endif

My implementation file:

#include "Shunt.h"

using namespace std;

void Shunt::shuntingYard(string &expr)
{
    ...
}

Please help I'm at the brink of chucking my laptop against the wall.

Was it helpful?

Solution

shuntingYard() is a non-static member function: you need an instance of Shunt on which to invoke it:

Shunt s;
s.shuntingYard(expr);

An alternative is make the member function static which does not require an instance of Shunt and can be invoked:

Shunt::shuntingYard();

Given that you deem it possible to invoke shuntingYard() without an instance making it static seems the more appropriate action. Or, if Shunt is being used to hold loosely related functions that share no state and do not represent the features of a specific abstraction is may be more appropriate to use a namespace instead of a class.

OTHER TIPS

shuntingYard is not a free function, but a member function. You'll need to call it from an instance of the class:

Shunt s;
s.shuntingYard(expr);

You can make it a static member of the class and call it as:

Shunt::shuntingYard(expr);

It is because you are calling it like a regular function when it is actually a member function and as such you need an instance of Shunt to call it on.

Main doesn't know what shuntingYard(expr) is. You'll have to create an object of type Shunt before you call shuntingYard(expr); on that object.

You've declared shuntingYard as the member function of an object but you're attempting to call it as if it was a free function.

You need to create a new Shunt class:

Shunt my_shunt;
my_shunt.shuntingYard(expr);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top