简体   繁体   English

使用结构简单的“标识符'xxx'未定义”

[英]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" 如果我尝试构建,则编译器错误为:“ test.cpp(22):错误C3861:'init':找不到标识符”

This is literally 100% of my code. 这实际上是我的代码的100%。 My #includes are there. 我的#includes在那。 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! 谢谢!

First of all, init is a method of class EuropeanOption , so you can call it from such object. 首先, initEuropeanOption类的方法,因此您可以从此类对象中调用它。 The second fact is that this method is private so it's impossible to call it outside the class. 第二个事实是此方法是private因此无法在类外调用它。 And thirdly, you have to use constructor to create objects. 第三,您必须使用构造函数创建对象。 Read some c++ books to understand it. 阅读一些c ++书籍以了解它。

init() is a member of a class (and it is private so it is not accessible anyway). init()是类的成员(并且是private因此无论如何都无法访问)。 main() is not a member of that class. main()不是该类的成员。 There is no init() function in the global scope, either. 全局范围中也没有init()函数。 That is why the compiler complaints about init() being undefined - it really is. 这就是为什么编译器会抱怨init()未定义-确实如此。 There is no defined init() function within main() 's scope. main()的范围内没有定义的init()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM