正在将链表添加到构造函数
Adding linked list to constructor
I;我真的不知道该怎么称呼我遇到的问题,但它已经困扰了我半个小时,现在我正试图解决它。我正试图将Item类作为清单传递到播放器类中的链接列表中,但我在这样做时遇到了困难。player.cpp没有;无法识别Item类并将其称为"class DLinkedList<>"。我也不确定是否将linkedlist添加到构造函数本身。
#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();
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()
{
}
- 你的玩家类成员应该(可能)是私人的
假设DLinkedList有一个不带参数的构造函数来构造一个空链表,那么就不需要做任何事情。编译器将添加对对象的默认构造函数的调用。
您将需要player::player( string name, int level, int experience, double maxweight, double currentweight, int health, int strenght, int defence, DLinkedList<Item> inventory)
构造函数tho'中的副本或赋值构造函数。
相关文章:
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 在 UML 类图中为C++类添加构造函数和析构函数
- 添加自定义析构函数时,Move 构造函数在派生类中消失
- C++将带有重载构造函数的对象添加到另一个对象
- c++17在编译时将带有已删除复制构造函数的类添加到std::vector
- 以支持继承的方式将自身shared_ptr添加到对象构造函数中的向量中
- 创建/向容器添加对象时如何使用构造函数/析构函数
- 如何在构造函数的静态列表中添加对象
- 为什么当我添加一个不同的对象(复制构造函数中的参数)时调用复制构造函数?
- 将参数添加到q_object构造函数
- 将功能添加到默认复制构造函数中
- C++构造函数期间向映射添加"self"
- 使用超载运算符添加构造函数
- 将元素添加到矢量时复制构造函数
- 找不到构造函数 - 如何将 CPP 文件添加到 Qt 构建过程
- 将类的每个实例添加到列表对象的构造函数
- 添加initializer_list构造函数后,构造函数调用的无声破坏
- 为什么添加移动构造函数禁用初始化列表
- 将构造函数添加到boost.python子类导致参数不匹配错误