我一直在尝试通过创建基于文本的游戏来学习 C++。在这个游戏中,我创建了一个 MapHandler,它有一个网格(多维数组,5x5,int)。我希望能够在调用类时将其传递给网格,但我似乎无法做到。
我的问题是:如何从外部为类中的数组设置值?
我写了一些代码来重现我的错误:
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
int age;
string characteristics[5];
Person();
};
int _tmain(int argc, _TCHAR* argv[])
{
string traits[5] = {'Stubborn','Ambitious','Smart','Emotional','Extrovert'};
Person Bob;
Bob.age = 18;
Bob.characteristics = traits;
system("Pause");
return 0;
}
最佳答案
在c++中,普通数组不是一等的,即它们不能被复制。你会受益于使用 std::array
相反。
关于C++:如何在类中启动数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21246192/