0

私は最近、C ++で個別のクラスファイルをいじり始めましたが、これが私の最初の試みでした:

まず、"ThisClass.h" というクラス ヘッダーを作成しました。

//ThisClass.h

#ifndef THISCLASS_H
#define THISCLASS_H

class ThisClass
{
private:
    int x;
    float y;

public:
    ThisClass(int x, float y);
    void setValues(int x, float y);
    int printX();
    float printY();
};
#endif // THISCLASS_H

次に、「ThisClass.cpp」というファイルにクラスを実装しました。

//ThisClass.cpp

#include "ThisClass.h"

ThisClass::ThisClass(int x, float y)
{
    this->x = x;
    this->y = y;
}

void ThisClass::setValues(int x, float y)
{
    this->x = x;
    this->y = y;
}

int ThisClass::printX()
{
    return this->x;
}
float ThisClass::printY()
{
    return this->y;
}

最後に、クラスを使用する「main.cpp」というファイルを作成しました。

//main.cpp

#include <iostream>

    using namespace std;

    int main()
    {
        ThisClass thing(3, 5.5);
        cout << thing.printX() << " " << thing.printY()<< endl;
        thing.setValues(5,3.3);
        cout << thing.printX() << " " << thing.printY()<< endl;
        return 0;
    }

次に、このプログラムをコンパイルして、MinGW コンパイラを使用するコード ブロックを使用して実行したところ、次のエラーが発生しました。

In function 'int main()':|
main.cpp|7|error: 'ThisClass' was not declared in this scope|
main.cpp|7|error: expected ';' before 'thing'|
main.cpp|8|error: 'thing' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

私はどういうわけかこれを間違っていますか?どんな助けでも大歓迎です。

4

2 に答える 2

2

を入れ忘れまし#include "ThisClass.h"main.cpp

于 2016-07-20T11:28:38.813 に答える
0

を入れるのを忘れたとすでに答えたよう#include "ThisClass.h"main.cpp

それを行うだけで、コードがコンパイルされます。私はあなたの質問に答えたいだけです - しかし、2 つの cout 呼び出しがあるにもかかわらず、今私のコンソールは何も出力していません関数のgetchar()returnに 置いてください。main

于 2016-07-20T12:13:40.143 に答える