创建单独的实现文件和头文件
Creating separate implementation and header files
//Game Lobby
//Simulates a game lobby where players wait
#include <iostream>
#include <string>
using namespace std;
class Player
{
public:
Player(const string& name = "");
string GetName() const;
Player* GetNext() const;
void SetNext(Player* next);
private:
string m_Name;
Player* m_pNext; //Pointer to next player in list
};
Player::Player(const string& name):
m_Name(name),
m_pNext(0)
{}
string Player::GetName() const
{
return m_Name;
}
Player* Player::GetNext() const
{
return m_pNext;
}
void Player::SetNext(Player* next)
{
m_pNext = next;
}
class Lobby
{
friend ostream& operator<<(ostream& os, const Lobby& aLobby);
public:
Lobby();
~Lobby();
void AddPlayer();
void RemovePlayer();
void Clear();
private:
Player* m_pHead;
};
Lobby::Lobby():
m_pHead(0)
{}
Lobby::~Lobby()
{
Clear();
}
void Lobby::AddPlayer()
{
//create a new player node
cout << "Please enter the name of the new player: ";
string name;
cin >> name;
Player* pNewPlayer = new Player(name);
//if list is empty, make head of list this new player
if (m_pHead == 0)
{
m_pHead = pNewPlayer;
}
//otherwise find the end of the list and add the player there
else
{
Player* pIter = m_pHead;
while (pIter->GetNext() != 0)
{
pIter = pIter->GetNext();
}
pIter->SetNext(pNewPlayer);
}
}
void Lobby::RemovePlayer()
{
if (m_pHead == 0)
{
cout << "The game lobby is empty. No one to remove!n";
}
else
{
Player* pTemp = m_pHead;
m_pHead = m_pHead->GetNext();
delete pTemp;
}
}
void Lobby::Clear()
{
while (m_pHead != 0)
{
RemovePlayer();
}
}
ostream& operator<<(ostream& os, const Lobby& aLobby)
{
Player* pIter = aLobby.m_pHead;
os << "nHere's who's in the game lobby:n";
if (pIter == 0)
{
os << "The lobby is empty.n";
}
else
{
while (pIter != 0)
{
os << pIter->GetName() << endl;
pIter = pIter->GetNext();
}
}
return os;
}
int main()
{
Lobby myLobby;
int choice;
do
{
cout << myLobby;
cout << "nGAME LOBBYn";
cout << "0 - Exit the program.n";
cout << "1 - Add a player to the lobby.n";
cout << "2 - Remove a player from the lobby.n";
cout << "3 - Clear the lobby.n";
cout << endl << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 0: cout << "Good-bye.n"; break;
case 1: myLobby.AddPlayer(); break;
case 2: myLobby.RemovePlayer(); break;
case 3: myLobby.Clear(); break;
default: cout << "That was not a valid choice.n";
}
}
while (choice != 0);
return 0;
}
大家好!我是C++的初学者,正在做这个问题。该问题提到以下代码,并要求提供某些解决方案。
问题要求:
测试类(playerTest.cpp(的驱动程序文件(主程序(由于这是一个实现和头的问题,我尝试研究这个主题。我看了thenewoston(Bucky(、MissouriSandTCourses、MicrosonicDev等公司的YouTube视频,但我没有任何概念。这本书(通过游戏编程开始c++(对这个主题没有任何帮助。
我也试着读了一些文章,但无济于事。
从当前源文件开始,进行增量更改。
首先,将class Player
的定义移动到名为player.h的文件中;那是一个头文件。然后将Player
的成员函数的定义移动到名为player.cpp的文件中;那是一个实现文件。
现在您当前的文件不会编译,因为它不知道class Player
。所以添加`#include"player.h"。
现在对class Lobby
执行相同的操作。
现在,原始源文件中只剩下int main()
和一些#include
指令。这是你的驱动程序文件。
以下是您的文件,分为头文件和实现文件。遵循这里定义之前声明的简单原则。头文件的Included#pragma once
作为include guard,在任何情况下,这些头文件都会多次包含到源文件中。我不建议在头文件中使用using namespace std
,请尝试将它们移到源文件中,并在头文件(如std::string
(中使用命名空间限定名称。
player.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
public:
Player(const string& name = "");
string GetName() const;
Player* GetNext() const;
void SetNext(Player* next);
private:
string m_Name;
Player* m_pNext; //Pointer to next player in list
};
播放器Imp.cpp
#include "player.h"
Player::Player(const string& name):
m_Name(name),
m_pNext(0)
{}
string Player::GetName() const
{
return m_Name;
}
Player* Player::GetNext() const
{
return m_pNext;
}
void Player::SetNext(Player* next)
{
m_pNext = next;
}
大厅.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player;
class Lobby
{
friend ostream& operator<<(ostream& os, const Lobby& aLobby);
public:
Lobby();
~Lobby();
void AddPlayer();
void RemovePlayer();
void Clear();
private:
Player* m_pHead;
};
lobbyImp.cpp
#include "lobby.h"
#include "player.h"
Lobby::Lobby():
m_pHead(0)
{}
Lobby::~Lobby()
{
Clear();
}
void Lobby::AddPlayer()
{
//create a new player node
cout << "Please enter the name of the new player: ";
string name;
cin >> name;
Player* pNewPlayer = new Player(name);
//if list is empty, make head of list this new player
if (m_pHead == 0)
{
m_pHead = pNewPlayer;
}
//otherwise find the end of the list and add the player there
else
{
Player* pIter = m_pHead;
while (pIter->GetNext() != 0)
{
pIter = pIter->GetNext();
}
pIter->SetNext(pNewPlayer);
}
}
void Lobby::RemovePlayer()
{
if (m_pHead == 0)
{
cout << "The game lobby is empty. No one to remove!n";
}
else
{
Player* pTemp = m_pHead;
m_pHead = m_pHead->GetNext();
delete pTemp;
}
}
void Lobby::Clear()
{
while (m_pHead != 0)
{
RemovePlayer();
}
}
播放器测试.cpp
#include "player.h"
#include "lobby.h"
ostream& operator<<(ostream& os, const Lobby& aLobby)
{
Player* pIter = aLobby.m_pHead;
os << "nHere's who's in the game lobby:n";
if (pIter == 0)
{
os << "The lobby is empty.n";
}
else
{
while (pIter != 0)
{
os << pIter->GetName() << endl;
pIter = pIter->GetNext();
}
}
return os;
}
int main()
{
Lobby myLobby;
int choice;
do
{
cout << myLobby;
cout << "nGAME LOBBYn";
cout << "0 - Exit the program.n";
cout << "1 - Add a player to the lobby.n";
cout << "2 - Remove a player from the lobby.n";
cout << "3 - Clear the lobby.n";
cout << endl << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 0: cout << "Good-bye.n"; break;
case 1: myLobby.AddPlayer(); break;
case 2: myLobby.RemovePlayer(); break;
case 3: myLobby.Clear(); break;
default: cout << "That was not a valid choice.n";
}
}
while (choice != 0);
return 0;
}
已编辑
请阅读以下文章以了解C++编译的更多信息。
https://en.wikipedia.org/wiki/Include_directive
http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compaile.html
https://www.toptal.com/c-plus-plus/c-plus-plus-understanding-compilation
- 在实现文件中使用头文件的通用 lambda
- 在文件上实现迭代器
- C++头文件和类实现出现问题
- 在文件夹迭代上实现 RAII
- 如何使用命令提示符、记事本和 MinGW 使用主文件、头文件和实现文件编译C++程序?
- 用于 Windows 写入临时文件的 mkstemp() 实现
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- Linux 源代码中普通磁盘文件的"轮询"功能在哪里实现?
- 在 *.cpp 文件中实现的 c++ 函数/方法永远不会内联扩展吗?
- 在实现文件中使用模板参数声明方法
- 在头文件中使用opencv类型来实现未定义的标识符
- 模板实现文件中的匿名命名空间
- 创建单独的实现文件和头文件
- 多文件类实现Cpp
- Reader类实现中的文件读取错误
- MEX文件实现特征库伪内函数崩溃
- openssl rc4 命令行加密和 cpp 文件实现 rc4 之间的区别
- 从其他头文件实现结构
- c++头文件-实现-头文件-实现依赖链
- 内存映射文件实现