0

プレーヤー クラスを実装しようとしているので、スレッド フォルダーに player.cc と player.h の 2 つのファイルを作成しました。

player.h は次のようになります。

#ifndef PLAYER_H
#define PLAYER_H
#include "utility.h"

class Player()
{
  public:
   //getPlayerID();
};

#endif

その後、player.ccは次のようになります

#include "player.h"

class Player()
{
  string playerID;
  int timeCycle;
}

次に、 main.cc と threadtest.cc に #include player.h を追加すると、エラーが発生してコンパイルに失敗します。私はナチョスが初めてで、c++ に少し慣れていないため、この問題を解決する方法について混乱しています。Nachos も、コンパイラーによる解決策を提供しません。

gmake と入力すると、2 つのエラーが表示されます。1. player.h の '(' の前の解析エラー (Player() を参照) 2. * [main.o] エラー 1

4

2 に答える 2

2

行ごとに見ていきましょう。

#ifndef PLAYER_H
#define PLAYER_H
#include "utility.h"

これまでのところ、コンパイラが をサポートしているかどうかを確認できます#pragma onceが、マクロは問題なく動作します。

class Player()

()はクラス名に使用できません。削除してください

{
  public:
   //getPlayerID();
};

#endif

ヘッダーファイルの残りの部分は問題ありません。実装ファイルを見てみましょう。

#include "player.h"

完全。プログラム全体で使用される定義が 1 つだけであることを確認するには、クラスをヘッダーに配置するのが最善の方法です。

class Player()

括弧は使用できませんが、ここでさらに大きな問題が発生します。その名前のクラスが既にあります。ヘッダーがクラス定義を提供するようにします。実装ファイルは、非インライン メンバー関数 (および任意のヘルパー コード) を提供するだけで済みます。

{
  string playerID;
  int timeCycle;
}

完全に修正されたバージョンは次のとおりです。

#if !defined(PLAYER_H)
#define PLAYER_H

#include <string>
#include "utility.h"

class Player
{
     std::string player_id;
     int time_cycle;

public:
     // this is how you make a constructor, the parenthesis belong here, not on the class name
     Player(std::string id, int time);

     std::string getPlayerId() const;
};

#endif /* !defined(PLAYER_H) */

および実装ファイル

#include "player.h"

// and this is how you write a non-inline constructor
Player::Player(std::string id, int time)
    : player_id(id)
    , time_cycle(time)
{}

std::string Player::getPlayerId() const
{
    return player_id;
}

これらの問題はすべて、本当に基本的な C++ の問題であり、NachOS とは関係ありません。

于 2011-09-26T01:34:54.523 に答える
1

ルート nachos ディレクトリの Makefile.common を変更しましたか? THREAD_HTHREAD_Oおよびに値を追加する必要があると思いますTHREAD_C

于 2012-12-03T02:54:34.507 に答える