1

私はほとんど新しいプロジェクトを開始しておらず、このエラーのために構造体データのバッチを単純に印刷することができません。コードは次のとおりです。

ヘッダーファイル:

#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

ソースファイル:

#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

テストファイル:

#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;
}

ビルドしようとした場合のコンパイラエラーは次のとおりです。"test.cpp(22):エラーC3861:'init':識別子が見つかりません"

これは文字通り私のコードの100%です。私の#includesがあります。私はそれを無駄にもっとユニークなものに名前を付けてみました。わかりません...ここで私のエラーを確認できますか?

ありがとう!

4

2 に答える 2

3

まず、initクラスのメソッドなEuropeanOptionので、そのようなオブジェクトから呼び出すことができます。2つ目の事実は、このメソッドはprivateクラス外で呼び出すことができないためです。そして第三に、オブジェクトを作成するにはコンストラクターを使用する必要があります。それを理解するためにいくつかのc++本を読んでください。

于 2012-10-07T16:38:45.733 に答える
2

init()はクラスのメンバーです(privateそのため、とにかくアクセスできません)。 main()そのクラスのメンバーではありません。init()グローバルスコープにも機能はありません。そのため、コンパイラは未定義について不満を言ってinit()います。実際はそうです。のスコープ内に定義されたinit()関数はありません。main()

于 2012-10-07T16:44:25.247 に答える