gpt4 book ai didi

关于假定未声明的函数的 C++ 编译器错误

转载 作者:行者123 更新时间:2023-11-28 03:30:48 25 4
gpt4 key购买 nike

我目前正在编写一个基本程序来评估数学表达式,稍后我将在遗传编程中使用它来确定表达式系统的最佳解决方案。我的编译器一直在提示,但我几乎可以肯定我做对了一切。

错误:

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 用于保存松散相关的函数,这些函数不共享状态且不代表特定抽象的功能,则使用命名空间而不是类可能更合适。

关于关于假定未声明的函数的 C++ 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12693148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com