-4

問題がありますが、オブジェクトを別のクラスに送信しようとしています。

エラー: コンストラクターで StartGame::StartGame(EntitySystem&):

エラー: クラス StartGame には ES という名前のフィールドがありません

メイン.cpp

#include <iostream>
#include "EntitySystem.h"
#include "StartGame.h"

using namespace std;

int main()
{
    EntitySystem ES;
    StartGame SG(ES);
    SG.Start();
    return 0;
}

StartGame.h

#ifndef STARTGAME_H
#define STARTGAME_H
#include "EntitySystem.h"
#include <iostream>

using namespace std;
class StartGame
{
public:
    StartGame(EntitySystem&);
    void ClassSelection();
    void Start();
    virtual ~StartGame();
};

StartGame.cpp

#include "StartGame.h"
#include "EntitySystem.h"
#include "windows.h"
#include "stdlib.h" // srand, rand
#include <iostream>
#include <ctime> //time

using namespace std;

StartGame::StartGame(EntitySystem& ES) : ES(ES)
{
}

私はいくつかのグーグルをしましたが、それを理解できませんでした。
前もって感謝します。

4

2 に答える 2

2

コンストラクターのmember-initialization-listESで、存在しない という名前のメンバーを初期化しようとしているため...

クラスの宣言でこれを行うことで修正できます:

class StartGame
{
    public:
        StartGame(EntitySystem&);
        void ClassSelection();
        void Start();
        virtual ~StartGame();
    protected:
    private:
        EntitySystem ES;
    //  ^^^^^^^^^^^^^^^^
};

// in the .cpp
StartGame::StartGame(EntitySystem& ES)
    : ES(ES)
//    ^^ Now this member exists
{
}

ただし、両方に同じ名前を付けると混乱する可能性があるため、パラメーターまたはメンバーのいずれかの名前を変更することをお勧めします...

于 2013-08-11T17:35:25.227 に答える
0

あなたのコードはこれを行います:

StartGame::StartGame(EntitySystem& ES)
    : ES(ES)

これを分解してみましょう:

":"       <-  means "Memberwise initialization"
"ES("     <-  means "call the constructor for member 'ES'"
"(ES)"    <-  these are the arguments to pass to the constructor
"{ ... }" <-  here is the constructor body itself.

問題は、クラスに「ES」というメンバーがないことです。

しかし、二次的な問題もあります。

"ES(ES)"

「ES」というメンバー変数がある場合、ここで競合が発生します。

将来このような問題を回避するために採用することができる、広く使用されているプラ​​クティスがいくつかあります。

  1. クラス、構造体、型の定義には大文字のキャメルケース ("SomeClass") を使用します。
  2. 非ローカル変数には接頭辞を使用します: メンバーには "m_"、静的変数には "s_"、グローバル変数には "g_" (一部の人は、ユニオンには "u_"、ユニオン メンバーには "um_" などを使用します)。
  3. 関数の引数には「_」サフィックスを使用します。void myFunction(int arg1_, int arg2_) { ...
  4. 関数には大文字を使用し、「getter」および「setter」関数の命名規則には規則を選択してください。

例えば

static size_t s_counter = 0; // count of how many things we've done.
extern size_t g_nowServing;  // imported from another module.

class Customer {
    size_t m_ticketNo;       // which ticket number I pulled
    std::string m_licensePlate;
public:
    Customer(size_t ticketNo_, const char* licensePlate_)
        : m_ticketNo(ticketNo_)
        , m_licensePlate("")
    {
        // somewhat artificial implementation for demonstrative purposes.
        char licensePlate[8] = "\0\0\0\0\0\0\0";
        for (size_t i = 0; i < sizeof(licensePlate); ++i) {
            if (!isalnum(licensePlate_[i]))
                throw CustomerException("invalid character in license plate");
            licensePlate[i] = licensePlate_[i];
        }
        if (licensePlate[sizeof(licensePlate) - 1] != '\0')
            throw CustomerException("invalid license plate -- too long");

        m_licensePlate = licensePlate;
    }
};

これにより、コード内の問題をすぐに診断することがはるかに簡単になります。

StartGame::StartGame(EntitySystem& es_)
    : m_es(es_)
{
}

これにより、コンパイラ エラーerror: class StartGame does not have any field named m_esとビンゴが発生し、問題をすぐに突き止めることができたはずです。

于 2013-08-11T18:13:08.543 に答える