0

我不知道该怎么称呼我遇到的问题,但它一直困扰着我半个小时,现在试图解决它。我正在尝试将 Item 类作为库存传递给播放器类中的链表,但我在这样做时遇到了麻烦。player.cpp 无法识别 Item 类并将其称为“class DLinkedList<>”。我也不确定将链表添加到构造函数本身。

#include "player.h"
#include "Item.h"
#include "DLinkedList.h"

// ----------------------------------------------------------------
//  Name:           Constructor
//  Description:    Constructor
//  Arguments:      
//  Return Value:   None.
// ----------------------------------------------------------------

player::player(){
    this ->name = "default";
    this ->lvl = 99;
    this ->exp = 99;
    this ->maxweight = 99;
    this ->currentweight = 99;
    this ->health = 99;
    this ->strenght = 99;
    this ->defence = 99;
    this -> //trying to add linked list here
}

player::player( string name, int level, int experience, double maxweight, double   currentweight, int health, int strenght, int defence, DLinkedList<Item> inventory){
    this ->name = name;
    this ->lvl = lvl;
    this ->exp = exp;
    this ->maxweight = maxweight;
    this ->currentweight = currentweight;
    this ->health = health;
    this ->strenght = strenght;
    this ->defence = defence;       
}

这里也是播放器标题

class player{
public:

//Data members
string name;
int lvl;
int exp;    
double maxweight;
double currentweight;
int health;
int strenght;
int defence;
DLinkedList<Item> inventory;

//Constructor
player();
player(string name, int level, int experience, double maxweight, double currentweight, int health, int strenght, int defence, DLinkedList<Item> inventory);
~player();
4

2 回答 2

0
this -> //trying to add linked list here

应该:

this ->inventory(); //trying to add linked list here

有几点需要注意:

  • 你不需要this->在会员面前。

  • 如果您使用初始化列表,您的代码会更好(更小更高效)

例子:

player::player():
     name("default"),
     lvl(99),
     // and so on
     inventory()
{
}
  • 你的玩家类的成员应该(可能)是私有的
于 2013-08-11T20:22:18.770 回答
0

假设 DLinkedList 有一个没有参数的构造函数来构造一个空链表,那么您不需要做任何事情。编译器将为对象添加对默认构造函数的调用。

您将需要在player::player( string name, int level, int experience, double maxweight, double currentweight, int health, int strenght, int defence, DLinkedList<Item> inventory)构造函数 tho' 中使用复制或赋值构造函数。

于 2013-08-11T20:28:45.563 回答