【发布时间】:2019-11-23 03:41:24
【问题描述】:
我正在尝试在 main 中调用公共函数 validInfixCheck(),但在尝试编译时出现此错误:
g++ 计算器Main.cpp CalculatorExp.cpp
In function `main':
calculatorMain.cpp:(.text+0x99): undefined reference to
`CalculatorExp::validInfixCheck(std::string)'
collect2: error: ld returned 1 exit status
注意:validInfixCheck() 现在不做任何事情。我只是想确保我可以在 main 中使用它。
我尝试调用一个没有参数的公共函数来验证这不是问题,并且出现了相同的错误。
calculatorMain.cpp
#include "CalculatorExp.h"
#include<iostream>
#include <string>
using namespace std;
//prototype declarations
string getInfixExpression();
int main()
{
CalculatorExp calc;
string inputExpression;
inputExpression = getInfixExpression();
calc.validInfixCheck(inputExpression);
return 0;
}
string getInfixExpression()
{
string exp;
cout<<"Enter infix expression to evaluate: "<<endl;
cin>>exp;
return exp;
}
CalculatorExp.cpp
#include "CalculatorExp.h"
#include <string>
#include <stack>
using namespace std;
CalculatorExp::CalculatorExp()
{
//default constructor
}
// public //
// valid input check
bool validInfixCheck(string inputExpression)
{
return 0;
}
CalculatorExp.h
#ifndef CALCULATOREXP_H
#define CALCULATOREXP_H
#include <string>
#include <stack>
using namespace std;
class CalculatorExp
{
public:
/** Default Constructor;
* @param none
* @pre None*/
CalculatorExp();
/** CONSTANT MEMBER FUNCTIONS*/
/** returns the exp.
/* @pre None
/* @post The value returned is the exp*/
string get_exp( ) const { return exp; }
/** FUNCTIONS*/
/** returns true if exp is validated.
/* @pre None
/* @post The value returned is true if exp is validated.*/
bool validInfixCheck(string inputExpression);
private:
/** expression*/
string exp;
};
#endif
【问题讨论】:
-
bool CalculatorExp::validInfixCheck(string inputExpression)而不是bool validInfixCheck(string inputExpression) -
在stackoverflow.com/a/12574407/981959查看“一个常见的错误是忘记限定名称”
标签: c++ function public-method