我是 C++ 编程的新手,以前对 AS3 编程有一定的了解。我的问题是我无法弄清楚如何将一个新对象从一个类插入到一个数组中。
基本上我想做的是:
ClassName classArray[];
classArray[n]=new ClassName("Tekst");
这是我的代码(使用 Visual Studios 2012 C++ 编写):
#include <iostream>
#include <string>
using namespace std;
//a class holding user data
class User
{
public:
string name;
User(string nameInn)
{
//when the user is created it should get information about its name.
name=nameInn;
}
};
//array with all users
User userArr[];
int userArrLength=0; //the length of that array (dont know how to find the length of arays holding classes)
int main()
{
//the user writes down the name of all users.
cout << "Write user name. \n Write \"fin\" til finish\n";
bool hasFinished=false;
//asks you for a new user until you write fin
while(hasFinished==false)
{
string inn;
cin >> inn;
if(inn=="fin") hasFinished=true;
//here im trying to make a new user inn a new spot in the userArr.
else userArr[(userArrLength+=1)+1]=new User(inn);
}
return 0;
}
我的格式是不是错了,如果是,我该如何格式化?还是我误解了 C++ 中类的一些基本要素?