0

我正在尝试实现一个播放器类,所以我在我的线程文件夹中创建了两个文件 player.cc 和 player.h

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 时,它​​会说两件事来表示错误。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"

完美的。将类放在标题中是确保在整个程序中只使用一个定义的最佳方法。

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_H一些THREAD_O价值THREAD_C

于 2012-12-03T02:54:34.507 回答