我正在尝试实现一个播放器类,所以我在我的threads文件夹中创建了两个文件, 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然后我开始出错并且无法编译。我是nachos的新手,对c ++有点不熟悉,所以我很困惑如何解决这个问题。 Nachos也没有通过编译器提供解决方案。
当我输入gmake时,它会说两件错误。 1.在player.h中的'('之前解析错误(指Player()) 2. * [main.o]错误1
答案 0 :(得分: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无关。
答案 1 :(得分:1)
您是否修改了根目录nachos目录中的Makefile.common?我认为您应该为THREAD_H
,THREAD_O
和THREAD_C
添加一些价值。