未宣言の関数に関する C++ コンパイラ エラー
-
12-12-2019 - |
質問
私は現在、数式を評価するための基本的なプログラムを作成しています。これは、後で遺伝的プログラミングで使用して、式のシステムに対する最適な解決策を決定します。私のコンパイラは文句を言い続けていますが、私はすべてが正しいとほぼ確信しています。
エラー:
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
私のメイクファイル:
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
私のメイン:
#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;
}
私のヘッダーファイル:
#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
私の実装ファイル:
#include "Shunt.h"
using namespace std;
void Shunt::shuntingYard(string &expr)
{
...
}
助けてください。ノートパソコンを壁にぶつける寸前です。
解決
shuntingYard()
は非ですstatic
メンバー関数:のインスタンスが必要です Shunt
それを呼び出す対象:
Shunt s;
s.shuntingYard(expr);
代わりにメンバー関数を作成することもできます static
のインスタンスを必要としない Shunt
そして呼び出すことができます:
Shunt::shuntingYard();
発動可能と判断した場合 shuntingYard()
インスタンスが作成されないまま static
より適切なアクションと思われます。あるいは、 Shunt
状態を共有せず、特定の抽象化の機能を表さない、緩やかに関連した関数を保持するために使用されます。クラスの代わりに名前空間を使用する方が適切な場合があります。
他のヒント
shuntingYard
無料の関数ではなく、メンバー関数です。クラスのインスタンスから呼び出す必要があります。
Shunt s;
s.shuntingYard(expr);
にすることができます static
クラスのメンバーとしてそれを次のように呼び出します。
Shunt::shuntingYard(expr);
通常の関数の場合は通常の関数のように呼び出しており、そのように呼び出すためにShunt
のインスタンスが必要です。
mainは、shuntingYard(expr)
が何であるかわからない。そのオブジェクトのShunt
を呼び出す前に、shuntingYard(expr);
のオブジェクトを作成する必要があります。
shuntingYard
をオブジェクトのメンバー関数として宣言しましたが、それが自由関数であるかのように呼び出すことを試みます。
新しいShunt
クラスを作成する必要があります。
Shunt my_shunt;
my_shunt.shuntingYard(expr);
.