我想创建一个类数组并打印它。
现在我的方法是:
这是我到目前为止所拥有的:
#include <iostream>
#include <string>
using namespace std;
const int invsize = 5;
//first class:
class Item {
private:
//i'm supposed to have all values set to private
string itname;
int itpower;
int itlasting;
public:
//this is so I can create an array of those classes later:
Item(string i, int j, int k) { itname = i; itpower = j; itlasting = k; }
//a method to print this class:
void itprint() {cout << itname << itpower << itlasting;};
~Item(){};
};
//second class:
class Player {
private:
string plname;
int plrole;
int plbrain;
int plbrawn;
//aggregation:
Item * Inventory;
public:
void plprint();
Player();
~Player();
};
//a Player constructor, in which I want to create the array of "Items" also:
Player::Player() {
/*I'm adding info to the Player class first, it works fine and is irrelevant to my question so I didn't paste it here*/
//now I'm creating an array of Item classes and giving them values:
Item Inventory[4] = { Item("generic name", 5, 5), Item("generic name", 5, 5),
Item("generic name", 5, 5), Item("generic name", 5, 5) };
}
//a method to print the Player class and the "Inventory" array:
void Player::plprint() {
/*printing the info about the Player class here first, again, works fine and is irrelevant so I didn't copypaste it*/
//printing the "Inventory" array:
cout << "Your inventory contains:" << endl;
for (int i=1;i<invsize;i++) {
Inventory[i].itprint();
}
}
//and finally the main:
int main () {
//creating a Player class - as I understand, the "Inventory" array should get created with it
Player * firstPlayer = new Player;
//printing the created object and its inventory
firstPlayer->plprint();
system("pause");
return 0;
}
程序编译,当我运行它时,它似乎正确地创建了Player类,它还打印了关于播放器的信息就好了,然后数组出了问题,整个事情就崩溃了。
我确定我在这里无法理解。我非常感谢任何帮助。我google了很多,但无法找到任何相关的教程或回答问题。先感谢您!原谅我的英语不好,这是我的第二语言。
答案 0 :(得分:0)
似乎正确创建了Player类
不,它没有。
Player::Player() {
/*I'm adding info to the Player class first, it works fine and is irrelevant to my question so I didn't paste it here*/
//now I'm creating an array of Item classes and giving them values:
Item Inventory[4] = { Item("generic name", 5, 5), Item("generic name", 5, 5),
Item("generic name", 5, 5), Item("generic name", 5, 5) };
}
您正在Player构造函数中创建一个名为Inventory的Item数组,这是一个局部变量,而不是指您的成员变量Inventory。
因此,当你的构造函数结束时,本地数组Inventory将被销毁......
那么,为什么要使用指针作为库存数组呢?您可以通过使用数组本身轻松完成此操作。
然后数组出现问题,整个事情就崩溃了。
void Player::plprint() {
/*printing the info about the Player class here first, again, works fine and is irrelevant so I didn't copypaste it*/
//printing the "Inventory" array:
cout << "Your inventory contains:" << endl;
for (int i=1;i<invsize;i++) {
Inventory[i].itprint();
}
}
这是因为你正在计算并从1开始打印数组,数组应该从0开始。所以你应该使用for (int i = 0; i < invsize - 1; ++i) {/*...*/}
重要的是,在C ++或几乎所有编程语言中,数组从0开始
您缺少类Item的默认构造函数,因此如果您在尝试声明Item Inventory或Item Inventory时会出现错误[4]
固定代码:
#include <iostream>
#include <string>
using namespace std;
const int invsize = 5;
//first class:
class Item {
private:
//i'm supposed to have all values set to private
string itname;
int itpower;
int itlasting;
public:
//this is so I can create an array of those classes later:
Item() {};
Item(string i, int j, int k) { itname = i; itpower = j; itlasting = k; }
//a method to print this class:
void itprint() { cout << itname << " " << itpower << " " << itlasting << std::endl; };
~Item() {};
};
//second class:
class Player {
private:
string plname;
int plrole;
int plbrain;
int plbrawn;
//aggregation:
Item Inventory [4];
public:
void plprint();
Player();
~Player();
};
//a Player constructor, in which I want to create the array of "Items" also:
Player::Player() {
/*I'm adding info to the Player class first, it works fine and is irrelevant to my question so I didn't paste it here*/
//now I'm creating an array of Item classes and giving them values:
for (int i = 0; i < 4; ++i)
{
Inventory[i] = Item("generic name", 5, 5);
}
};
//a method to print the Player class and the "Inventory" array:
void Player::plprint() {
/*printing the info about the Player class here first, again, works fine and is irrelevant so I didn't copypaste it*/
//printing the "Inventory" array:
cout << "Your inventory contains:" << endl;
for (int i = 0; i<invsize - 1; i++) {
Inventory[i].itprint();
}
}
//and finally the main:
int main() {
//creating a Player class - as I understand, the "Inventory" array should get created with it
Player * firstPlayer = new Player;
//printing the created object and its inventory
firstPlayer->plprint();
system("pause");
return 0;
}