簡體   English   中英

C ++重載構造函數問題

[英]C++ Overloaded Constructor issues

我正在嘗試在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'

您忘記了:

#include "DLinkedList.h"

在player.h中 這會導致第22行出現錯誤:

DLinkedList<Item> inventory;

因為DLinkedList對於編譯器是未知的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM