-2

クラスを別のファイルに配置し、main. 以下は私の簡単なコードです。

getKey()関数をどのように使用できるか疑問に思っていますint main()

#include "stdafx.h"
#include <iostream>
#include <string>
#include "TravelFunctions.h"


using namespace std;

TravelFunctions::getKey()

{
    cout << "i am a bananna" << endl;
}

私のTravelFunction.hクラス

class TravelFunctions

{
    public:
           getKey();

}

私のメインクラス

#include "stdafx.h"
#include <iostream>
#include <string>
#include "TravelFunctions.h"


using namespace std;

int main()
{
    getKey bo;
    return 0;

}
4

2 に答える 2

3

まず、クラスからオブジェクトをインスタンス化する必要があります。メイン関数は次のようになります。

int main()
{
    TravelFunctions functions;

    functions.getKey();

    return 0;
}

また、関数の戻り値の型として void を定義する必要があります。

.cpp:

void TravelFunctions::getKey()
{
    cout << "i am a bananna" << endl;
}

.h:

class TravelFunctions
{
    public:
           void getKey();

}; // Notice that you have to add ; after the class definition
于 2013-05-02T12:51:54.213 に答える
1
TravelFunctions obj;
obj.getKey();

メンバー関数を呼び出すには、クラス インスタンスが必要です。

きっといい本が手に入るはずです。
The Definitive C++ Book Guide and List

于 2013-05-02T12:52:09.400 に答える