在 main 中调用 public 函数

Issue calling public function in main

我试图在 main 中调用 public 函数 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() 现在没有做任何事情。我只是想确保我可以在 main 中使用它。

我试过调用一个没有参数的 public 函数来验证这不是问题,但还是出现了同样的错误。

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 

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

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