[英]C++ How can I fill an array of class 1 with objects of class 2?
我是 C++ 编程的新手,并试图学习如何将“猎人”类的对象添加到“世界”类的数组中。 我已经向“World”类添加了一个数组,它应该存储“Hunter”对象。 使用 '.createHunters()' 函数,我在 'world' 类中创建了一个 Hunter 数组,它保存着猎人对象,并试图将它传递给 'world' 类中的 Hunters-array。 这是我的课程和主要功能:
WORLD HEADER
#ifndef WORLD_H
#define WORLD_H
#include <iostream>
using namespace std;
class World
{
public:
World(int x, int y);
void printWorld();
void createHunters();
virtual ~World();
protected:
private:
int arrLength, arrWidth;
int arrWorld[1][1];
int arrHunters[8];
};
#endif // WORLD_H
-----
WORLD SOURCE
#include "World.h"
#include "Hunter.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
World::World(int x, int y) //constructor
{
arrLength = x;
arrWidth = y;
arrWorld[arrLength][arrWidth]; //pass on size of array
int k=0;
for(int i=0; i<arrLength; i++){
for(int j=0; j<arrWidth; j++){
arrWorld[i][j]=k;
}
}
}
void World::printWorld()
{
for(int i=0; i<arrLength; i++){ //print arrWorld
for (int j=0; j<arrWidth; j++){
cout << arrWorld[i][j] << "\t";
}
cout << endl;
}
}
void World::createHunters()
{
class Hunters;
Hunter Hunters[8];
for(int i=0; i<8; i++){
Hunters[i].addPoints(i+1);
}
for(int i=0; i<8; i++){
arrHunters[i] = Hunters[i];
}
}
World::~World()
{
//dtor
}
-----
HUNTER HEADER
#ifndef HUNTER_H
#define HUNTER_H
#include <iostream>
using namespace std;
class Hunter
{
public:
Hunter();
virtual void addPoints(int a);
virtual void printHunter();
virtual ~Hunter();
protected:
private:
int Hunternumber;
int lifePoints;
int gamePoints;
};
#endif // HUNTER_H
-----
HUNTER SOURCE
#include "Hunter.h"
#include <iostream>
using namespace std;
Hunter::Hunter()
{
cout << "Hunter " << Hunternumber << " constructed." << endl;
}
void Hunter::addPoints(int a)
{
Hunternumber=a;
lifePoints=5;
gamePoints=0;
cout << "added Points to Hunter " << Hunternumber << "/ LifePoints: " << lifePoints << "/ GamePoints: " << gamePoints << endl;
}
void Hunter::printHunter()
{
cout << "created Hunter " << Hunternumber << "/ LifePoints: " << lifePoints << "/ GamePoints: " << gamePoints << endl;
}
Hunter::~Hunter()
{
cout << "destructed Hunter " << Hunternumber << endl;
}
-----
MAIN-FUNCTION
#include <iostream>
#include "World.h"
#include "Hunter.h"
using namespace std;
int main()
{
int length, width;
cout << "Enter length of Map: " << endl; //get length of Map
cin >> length;
cout << "Enter width of Map: " << endl; //get width of Map
cin >> width;
class Wrld1; //creating World class
World Wrld1(length, width); //adding the size to created World array
Wrld1.printWorld();
Wrld1.createHunters();
我还处于游戏的开始阶段,我只是无法做到正确。 有任何想法吗? 我能做些什么来使这个运行,我做错了什么?
int arrHunters[8]
是一个连续块中的 8 个int
对象。 你不能把Hunter
对象放在那里。
int arrWorld[1][1];
是 1 个具有恼人语法的int
对象。 您不能在运行时放大它。
如果你想要的数组Hunter
中的对象World
,定义Hunter
必须先定义World
如果要在运行时调整大小,请使用std::vector
。
#ifndef WORLD_H
#define WORLD_H
#include "Hunter.h"
#include <array>
#include <vector>
class World
{
public:
World(int x, int y);
void printWorld();
protected:
private:
std::vector<std::vector<int>> arrWorld;
std::array<Hunter, 8> arrHunter;
};
#endif // WORLD_H
#include "World.h"
#include <iostream>
World::World(int x, int y) //constructor
: arrWorld(x, std::vector<int>(y, 0)),
arrHunter{ 0, 1, 2, 3, 4, 5, 6, 7 }
{ }
void World::printWorld()
{
for(std::vector<int> & col : arrWorld){ //print arrWorld
for (int value : col){
std::cout << value << "\t";
}
std::cout << std::endl;
}
}
您不需要(也不应该)声明一个与您将要声明的局部变量同名的类。 (即掉落class Hunters;
, class Wrld;
)
您不应该在初始化成员之前使用它们。 你的程序有未定义的行为,因为
Hunter::Hunter()
{
cout << "Hunter " << Hunternumber /* using an uninitialised value is undefined */ << " constructed." << endl;
}
您可能应该将Hunter::addPoints
的逻辑移动到它的构造函数中。
#ifndef HUNTER_H
#define HUNTER_H
class Hunter
{
public:
Hunter(int a);
void printHunter();
protected:
private:
int Hunternumber;
int lifePoints = 5;
int gamePoints = 0;
};
#endif // HUNTER_H
#include "Hunter.h"
#include <iostream>
Hunter::Hunter(int a) : Hunternumber(a) { }
void Hunter::printHunter()
{
std::cout << "created Hunter " << Hunternumber << "/ LifePoints: " << lifePoints << "/ GamePoints: " << gamePoints << std::endl;
}
您似乎需要做三件主要的事情。
首先是改变World的int arrHunters[8];
给Hunter arrHunters[8];
你想要一个猎人数组,而不是一个数字数组。
接下来,为了让 World.h 知道猎人到底是什么,在 World.h 的顶部,您需要#include "Hunter.h"
简短的切线:您正在使用class World;
和class Hunter;
有点奇怪,但我假设有人这样教你,我不会判断。 我想说的是我是如何学习的:如果两个 .h 文档都相互引用,它们就会争论一个必须首先编译的文件。 他们的争论通过放置class World;
可以最快速、最干净地解决class World;
在世界文件的顶部,和class Hunter;
在猎人文档的顶部,在所有包含语句的上方,编译器将在确定谁引用谁之前看到它们。 然后,编译器会预先警告每个单词代表一个类,如果它看到“World”或“Hunter”,并且还没有完全读取该类的相关 .h 文件,编译器就会知道该怎么做。
继续前进,由于大写和复数(以及将class
语句隐藏在函数内部而不是在任何更合理的区域),您稍后犯了一些错误。 错误在于以下功能:
void World::createHunters()
{
class Hunters;
Hunter Hunters[8];
for(int i=0; i<8; i++){
Hunters[i].addPoints(i+1);
}
for(int i=0; i<8; i++){
arrHunters[i] = Hunters[i];
}
}
你能看见它吗? 一开始就对了。 你不小心写了class Hunters;
而不是class Hunter;
“猎人”实际上是您的变量名称。 您可以通过始终保持变量以小写字母开头来帮助自己。 所以:
class Hunter;
Hunter Hunters[8]; //Change this to Hunter hunters[8]; and update everywhere else accordingly.
以上被认为在视觉上更明显,因为它可以帮助您在心理上区分大写的类和小写的成员/变量。
看看这些更改是否让您开始解决其余的代码,您做得很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.