C++创建一个类数组并打印它

C++ creating an array of classes and printing it

本文关键字:数组 打印 一个 创建 C++      更新时间:2023-10-16

我想创建一个类数组并打印它。

现在我的方法是:

  1. 创建两个不同的类
  2. 使用聚合
  3. 使用函数创建其中一个类的数组
  4. 使用函数打印该数组
  5. 所有函数都必须是void类型

以下是我迄今为止所拥有的:

#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类,它还很好地打印了关于播放器的信息,然后数组出现了问题,整个程序崩溃了。

我肯定我就是听不懂。如果有任何帮助,我将不胜感激。我在谷歌上搜索了很多,但找不到任何相关的教程或回答问题。提前谢谢!另外,请原谅我英语不好,这是我的第二语言。

似乎正确创建了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]时,if会给您一个错误

固定代码:

#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;
}