C++创建一个类数组并打印它
C++ creating an array of classes and printing it
我想创建一个类数组并打印它。
现在我的方法是:
- 创建两个不同的类
- 使用聚合
- 使用函数创建其中一个类的数组
- 使用函数打印该数组
- 所有函数都必须是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;
}
相关文章:
- 动态分配的聊天数组打印缺失的数据和空
- 对象数组打印空白字符串
- 数组打印"random"值时出现问题
- 数组打印出垃圾值
- 数组打印奇怪的字符,例如 ∟ @
- 测量我的随机数数组打印的时间始终显示为 0 秒
- 数组打印出错误的数字
- 从对象数组打印字符串对象
- 您好,如何防止此函数使用质数数组打印 1
- 如何从主内部的数组打印总计
- 如何将数组打印为矢量形式
- 有人能给我一些关于在C++中使用数组打印出丰富、完美和不足的数字的提示吗
- 为什么我的C++数组打印相同的值
- 如何阻止我的数组打印地址
- 将值输入到结构数组 + 打印输出
- C++11 为什么 cout 从布尔数组打印大整数
- 从字符串数组打印其中一个字符串时出现空指针赋值错误
- 将无符号字符数组打印为十六进制
- 缩短的数组打印错误的结果
- C++中的字符串数组:打印额外的字符