[英]C++ Overloaded Constructor issues
I'm trying to create an overloaded constructor inside my player.cpp but it's giving me the error that there is no instance of overloaded function "player:player" matches the specified type. 我正在尝试在player.cpp中创建一个重载的构造函数,但它给我一个错误,即没有任何重载函数“ player:player”实例与指定类型匹配。
Here is the header for reference 这是标题供参考
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include <iostream>
#include "Item.h"
using namespace std;
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();
};
#endif
and here is the .cpp 这是.cpp
#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 ->inventory;
}
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;
this ->inventory = inventory;
}
Here is the main where I create the player object 这是创建播放器对象的主要位置
DLinkedList<player> list;
DLinkedList<Item> inventory;
//creates and appends a few default players to the linked list
player player1 = player("Jeremy", 2, 4 ,95, 5, 4, 3, 2, inventory);
list.Append(player1);
I am pretty sure that is wrong however since I want each inventory to be unique to each player. 我很确定这是错误的,因为我希望每个玩家的库存都独一无二。
Here is some of the output errors related to the issue 这是与此问题相关的一些输出错误
c:\users\liam\desktop\ca2 liam\doublelinkedlistca2\doublelinkedlistca2\player.h(22): error C2143: syntax error : missing ';' before '<'
1>c:\users\liam\desktop\ca2 liam\doublelinkedlistca2\doublelinkedlistca2\player.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\liam\desktop\ca2 liam\doublelinkedlistca2\doublelinkedlistca2\player.h(22): error C2238: unexpected token(s) preceding ';'
1>c:\users\liam\desktop\ca2 liam\doublelinkedlistca2\doublelinkedlistca2\player.h(26): error C2061: syntax error : identifier 'DLinkedList'
1>c:\users\liam\desktop\ca2 liam\doublelinkedlistca2\doublelinkedlistca2\player.cpp(21): error C2039: 'inventory' : is not a member of 'player'
1> c:\users\liam\desktop\ca2 liam\doublelinkedlistca2\doublelinkedlistca2\player.h(10) : see declaration of 'player'
1>c:\users\liam\desktop\ca2 liam\doublelinkedlistca2\doublelinkedlistca2\player.cpp(24): error C2511: 'player::player(std::string,int,int,double,double,int,int,int,DLinkedList<Datatype>)' : overloaded member function not found in 'player'
You forgot to: 您忘记了:
#include "DLinkedList.h"
in player.h. 在player.h中 That causes an error in line 22: 这会导致第22行出现错误:
DLinkedList<Item> inventory;
because DLinkedList
is unknown to the compiler. 因为DLinkedList
对于编译器是未知的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.