C ++重载的构造函数问题

时间:2013-08-14 15:21:14

标签: c++ constructor

我正在尝试在我的player.cpp中创建一个重载的构造函数,但是它给了我一个错误,即没有重载函数的实例“player:player”匹配指定的类型。

以下是参考标题

#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

这是.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;
}

这是我创建播放器对象的主要位置

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);

我很确定这是错误的,因为我希望每个广告资源对每个玩家都是唯一的。

以下是与问题相关的一些输出错误

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'

1 个答案:

答案 0 :(得分:4)

你忘了:

#include "DLinkedList.h"

在player.h中。这导致第22行出错:

DLinkedList<Item> inventory;

因为编译器不知道DLinkedList