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.
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.