创建单独的实现和头文件

时间:2018-07-24 13:50:01

标签: c++ class header implementation

//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 LOBBY\n";
        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)的驱动程序文件(主程序)
  • 播放器类标题和实现文件-单独的接口和实现(player.h和playerImp.cpp)
  • 大厅类标题和实现文件-单独的接口和实现

由于这是一个实现和标头问题,所以我尝试研究此主题。我观看了thenewboston(Bucky),MissouriSandTCourses,MicrosonicDev等的YouTube视频,但我一无所获。这本书(通过游戏编程开始c ++)对这个主题完全没有帮助。

我也尝试阅读一些文章,但没有帮助。

2 个答案:

答案 0 :(得分:0)

从您当前的源文件开始,进行增量更改。

首先,将class Player的定义移动到名为player.h的文件中;那是一个头文件。然后将Player成员函数的定义移到名为player.cpp的文件中;那是一个实现文件。

现在您的当前文件将无法编译,因为它不了解class Player。因此,在其中添加#include“ player.h”。

现在为class Lobby做同样的事情。

现在,原始源文件中只剩下int main()和一些#include指令。那是您的驱动程序文件。

答案 1 :(得分:0)

以下是您的文件,分为标头和实现。遵循声明之前定义的简单原则。头文件的包含// You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("1.3.*.*")] //[assembly: AssemblyFileVersion("1.0.0.0")] not used 作为包含保护,无论如何都将它们多次包含到源文件中。我不建议在头文件中使用#pragma once,请尝试将它们移至源文件并在诸如using namespace std的头中使用名称空间限定名称。

player.h

std::string

playerImp.cpp

#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
};

lobby.h

#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;
}

lobbyImp.cpp

#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;  
};

playerTest.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();
    }
}

已编辑

请阅读以下文章,以进一步了解C ++编译。

https://en.wikipedia.org/wiki/Include_directive

http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html

https://www.toptal.com/c-plus-plus/c-plus-plus-understanding-compilation