-2

こんにちは、directX を使用して C++ でゲームを作成しています

しかし、私はゲームのコンソール版も持っています ('X' やその他の文字を使用して物事を表します)

次のコードがあります。

Unit::Unit(UnitType u){
    ZeroAll();                        // function that zeros all variables
    this->operator=(fileToUnit(u));   // error making code
}

UnitType は、3 つの値 {Infantry、Alien、Predator} を持つ単純な列挙データ型です。

Operator 関数は次のように定義されます。

Unit operator= (Unit u) { return u; }

fileToUnit は...

Unit fileToUnit(UnitType u);

一時的なユニットを作成して返すだけです。他にどうすればいいのか本当にわかりませんが、クラス全体をコンストラクターから変更する必要があります。

編集:具体的でなくてごめんなさい

私の質問は: 関数の結果に基づいてクラスの値を変更する方法

お気に入り

this = functionReturningSameDataType( DataType ConstructorParameters );

エラーは次のとおりです

Microsoft Visual C++ Runtime Library

Debug Assertion Failed!

Program: C:\Windows\system32\MSVCP110D.dll
File: C:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring
Line: 1143

Expression: invalid null pointer

For Information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to Debug the application)
4

1 に答える 1

0

あなたの質問を正しく理解していれば、引数としてa を取り、おそらくインスタンスに基づいてa を構築し、これをコンストラクターに返すfree 関数があります。UnitTypeUnitUnitTypeUnit::Unit(UnitType)

Unit fileToUnit(UnitType u)このコンテキストでの目的を完全に理解しているかどうかはわかりません。なぜ無料の機能なのですか?それをメンバー関数にして、コンストラクターから呼び出します。

class Unit
{
private:
   // Example data members
   int foo;
   int bar;
public:
   Unit(UnitType u)
   {
      fileToUnit(UnitType u);
   };
private:
   void fileToUnit(UnitType u)
   {
       // Set data members here
       foo = 1;
       bar = 1;
   };
};

編集:私のような単純化された例でfileToUnitは、まったく存在する必要はなく、コードはコンストラクターに直接入る必要があります:

class Unit
{
private:
   // Example data members
   int foo;
   int bar;
public:
   Unit(UnitType u)
   {
      // Set foo and bar here, using whatever
      // information you need from u
   };
};
于 2013-03-18T23:03:42.367 に答える