variables to constructor

I wanted to fill a vector with instances of a class named NEO like this:

  
  
vector<NEO> activeNEOs;  
  
for(int i=0; i<50; i++){  
    activeNEOs.push_back(new NEO());  
}  
  

I did something similar in Processing which worked just fine but now it doesn’t work; so I tried this instead:

  
  
vector<NEO> activeNEOs;  
  
for(int i=0; i<50; i++){  
    activeNEOs.push_back(*new NEO());  
}  
  

The program compiles if I do it this way even though I’m not sure this is the correct thing to do (I’m putting pointers into the vector now, right?).

After that I wanted to send some variables to the contructor of the newly created object:

  
  
for(int i=0; i<50; i++){  
    activeNEOs.push_back(*new NEO(data,i));  
}  
  
//NEO.h  
class NEO{  
        NEO(string[][],int);  
}  
  
//NEO.cpp  
NEO::NEO(string[][] tempData,int tempI){  
}  
  

data is a two-dimensional array and i is an int, but the program no longer compiles, gives the error 'no matching function for call to ‘NEO::NEO(std::string [(((unsigned int)(((int)((testApp*)thi…’ and I just don’t know what I’m doing anymore.

I’m not sure if I’m creating the instances of the NEO class the right way and I’m not sure if I’m using the constructor the right way :slight_smile:
Any help would be greatly appreciated. I’ve already spend too many hours searching for help on a seemingly easy piece of coding.

Hi Thark,

Try this. It makes a local instance of NEO en stores a copy of it in the vector.

  
  
vector<NEO> activeNEOs;  
  
for(int i=0; i<50; i++){  
    NEO neo;  
    activeNEOs.push_back(neo);  
}  
  

For the constructor arguments you can do this:

  
  
vector<NEO> activeNEOs;  
  
for(int i=0; i<50; i++){  
    NEO neo(data,i);  
    activeNEOs.push_back(neo);  
}  
  

You can also store pointers to NEO objects in the vector but then you have to manually free memory when you’re done with the objects.

  
  
vector<NEO*> activeNEOs;  
  
for(int i=0; i<50; i++){  
    activeNEOs.push_back(new NEO(data,i));  
}  
  

I hope this helps.

That’s great, it all worked perfectly. I was able to make a vector with the instances and pass information to the constructor. I don’t really need to use the pointers right now but I’ll save your example for later use.

Thank you very much!