【问题标题】:Trying to start queue for tracking players turn尝试启动队列以跟踪玩家转弯
【发布时间】:2018-11-15 17:26:30
【问题描述】:

我正在尝试创建一个队列来跟踪玩家的回合。队列中可能有 8 个玩家,因此队列中应该有 8 个 Player 对象。我不断收到的错误是LNK2005 "public:_thiscall Player::Player(void)" (??0Player@@QAE@XZ) already defined in Main.objLNK1169 one or more multiply defined symbols found。我在网上找到的唯一答案是尝试让我的吸气剂内联,但这并没有真正帮助。我还尝试在没有循环的情况下制作 4 个不同的 Player 对象并单独添加它们,但这也没有解决任何问题。最初,Player 类是一个模板类,但我更改了它,因为我认为它不需要是一个,并且当它是一个不同的错误时我得到了一个不同的错误。希望有人可以提醒我我做错了什么。队列是一个模板,因为我希望能够在队列中存储任何内容。所以,首先我初始化了一个队列。

//initialize players turn in queue
Queue<Player> playerQueue(NUM_PLAYERS);

//track players added
int playerNum = 1;

//keep looping until we reached 
//the number of players
while (playerNum <= NUM_PLAYERS) {

    //create new object
    Player newPlayer;
    //add to queue
    playerQueue.add(newPlayer);
    //increment player count
    playerNum++;
}

目前我的播放器类非常简单。 getter 被注释掉,因为目前它们被内联在头文件中。阅读这样做可能会解决我的问题。它没有。

#include <iostream>
#include "player.h"
#include "queue.h"
using namespace std;

Player::Player() {

    pName = "Outis";
    queuePos = 0;
    propSize = 0;
    currency = 0;
}

//Getters
/*int Player::getCurrency() {
    return currency;
}

int Player::getQueuePos() {
    return queuePos;
}*/

这是我的头文件

 class Player {

public:
    Player();

    //Getters
    inline int getCurrency() { return currency; }
    inline int getQueuePos() { return queuePos; }
    //vector<Properties> getProperties();

private:

    //players name
    std::string pName;

    //when it is their turn
    int queuePos;

    //size of property vector
    int propSize;
    //vector to store properties
    //vector<Properties> propList;

    //amount of currency
    int currency;
};

这是我的队列模板类中的添加函数

// Add value to rear
template<class T>
void Queue<T>::add(const T &val)
{
    if (numStored == capacity)
    {
        cerr << "Queue full: cannot add element" << endl;
        return;
    }

    // add to rear
    int rearIndex = (frontIndex + numStored) % capacity;

    elements[rearIndex] = val;
    numStored++;

}

【问题讨论】:

  • 1.删除头文件中全局声明的所有变量。 2. 使用某种类型的include guards。 3. 确认您没有在构建过程中多次包含源文件。 4. 使用命名空间。
  • 学习使用现有代码,例如std::queue。现有代码将节省时间,因为它已经为您调试过了(尽管已经编写好了)。
  • @ThomasMatthews 我检查了一下,头文件中没有全局变量(除非您指的是链接列表的结构,但我认为这不重要),#ifndef,#define, #endif,那些是你的意思包括守卫吗?我有那些。我检查了我的 .cpp 文件,他们调用他们的 .h 文件,main.cpp 调用其他 .cpp 文件,就是这样。我的老师一直告诉我不要使用命名空间,只需使用“使用命名空间”虽然你不是第一个告诉我的人。在我更改为使用内置的 std::queue 后,它仍然会发生。 =(

标签: c++ object queue


【解决方案1】:

发现我的问题...

我检查了我的 .cpp 文件,他们调用他们的 .h 文件,main.cpp 调用其他 .cpp 文件

必须在 main 中包含 .h 文件,而不是 .cpp。不太明白,因为我为我制作的 Queue 模板类添加了 .cpp 而不是 .h 类。但是,问题已解决,因此我没有触摸它。谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多