在主函数中调用公共函数时出现问题

Issue calling public function in main

本文关键字:函数 问题 调用      更新时间:2023-10-16

我正在尝试在main中调用公共函数validInfixCheck(),但是在尝试编译时遇到此错误:

g++ calculatorMain.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()现在不执行任何操作。我只是想确保我可以在主要情况下使用它。

我尝试调用一个没有参数的公共函数来验证这不是问题并且出现相同的错误。

计算器主.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;
}

计算器经验.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;
}

计算器经验

#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 

您已在 CalculatorExp.h 中声明 validInfixCheck(( 作为类 CalculatorExp 的方法。但是,您尚未将此函数定义为类的成员,因为您在定义中省略了类名前缀。因此,请在 CalculatorExp.cpp 中进行此更改:

bool CalculatorExp::validInfixCheck(string inputExpression)
{
    return 0;
}