i'm new to c++ programming, with previous knowledge about AS3 programming. My problem is that i cannot figure out how to insert a new object form a class into an array.
Basically what i'm trying to do is:
ClassName classArray[];
classArray[n]=new ClassName("Tekst");
Here is my code (written using 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;
}
Is my formatting just wrong, if so how do i format it? Or have i misunderstood something essential about classes in C++?
newat all?