Search code examples
c++structidentifier

simple "identifier 'xxx' is undefined" using a struct


I'm barely starting a new project, and I'm unable to simply print a batch of struct data, because of this error. Code is as follows:

Header file:

#ifndef EuropeanOption_HPP
#define EuropeanOption_HPP

#include <iostream>
#include <string>

using namespace std;    

struct EquityParms
{
    double T; // years until expiry
    double K; // strike price
    double sig; // vol
    double r; // risk free rate
    double S; // current equity price
};

class EuropeanOption
{
private:
    void init(const struct EquityParms data); // initialize EquityParms 

public:

};


#ifndef EUROPEANOPTION_CPP
#include "EuropeanOption.cpp"
#endif

#endif

Source file:

#ifndef EUROPEANOPTION_CPP
#define EUROPEANOPTION_CPP

#include "EuropeanOption_H.hpp"


void EuropeanOption::init(const struct EquityParms data)
{
    cout << "Years until expiry: \t" << data.T << endl;
    cout << "Strike price: \t" << data.K << endl;
    cout << "Volatility: \t" << data.sig << endl;
    cout << "Risk-free rate: \t" << data.r << endl;
    cout << "Current equity price: \t" << data.S << endl;
}

#endif

Test file:

#include "EuropeanOption_H.hpp"

int main()
{

    struct EquityParms batch1 = {0.25, 65, 0.30, 0.08, 60};
    struct EquityParms batch2 = {1, 100, 0.2, 0.0, 100};
    struct EquityParms batch3 = {1, 10, 0.5, 0.12, 5};
    struct EquityParms batch4 = {30, 100, 0.30, 0.08, 100};

    init(batch1); // error on this line, "identifier init is undefined"

    return 0;
}

The compiler error if I try to build is: "test.cpp(22): error C3861: 'init': identifier not found"

This is literally 100% of my code. My #includes are there. I tried simply naming it something more unique to no avail. I don't get it... Could you see what's my error here?

Thanks!


Solution

  • init() is a member of a class (and it is private so it is not accessible anyway). main() is not a member of that class. There is no init() function in the global scope, either. That is why the compiler complaints about init() being undefined - it really is. There is no defined init() function within main()'s scope.