c++functionpublic-method

Issue calling public function in main


I'm trying to call the public function validInfixCheck() in main but I'm getting this error when trying to compile:

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

NOTE: validInfixCheck() doesn't do anything right now. I just want to make sure I can use it in main.

I have tried calling a public function that doesn't have a parameter to verify that that is not a problem and the same error shows up.

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 

Solution

  • You have declared validInfixCheck() as a method of class CalculatorExp in CalculatorExp.h. However, you have not defined this function as a member of the class since you have left out the class name prefix in the definition. So make this change in CalculatorExp.cpp:

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