pointer and constructor

#include<iostream>
const int size = 2;
using namespace std;
class Human
{
protected:
string name;
public:
Human(string);
string setName(string);
string getName();

};
int main()
{

Human *p = new Human[2] ={Human("BBBB"),Human("CCCC")}

return 0;
}
Human::Human(string name)
{
this -> name = name;
cout << " The Object is now created ! " << endl;
}
string Human::setName( string name)
{
name = name;
}

string Human::getName()
{
return name;
}

Hi
I want use dynamic array when create a object , but when use a dynamic array compiler return error . what is my problem ???
What is your error? Although it may seem obvious and easy enough for us to copy and paste the code you provided, and at first that's exactly what I did, we don't often do this and I my self have stopped for a couple of reasons. Please let us know what your compiler is complaining about and maybe what compiler you are using.
Hi
my compiler is code-block, and return no matching function for call to Human::Human()'|
It looks like it wants a default constructor. I don't think this is manditory but try adding one anyway for appeasment reasons.
I can't use a constructor ?
No you CAN use a constructor, I think it wants another one called a default constructor
When you allocate with new[], you must use the default ctor. You cannot use any other ctor with new[].

It's a bummer, I know.
Hi . i solved my probleam with default ctor .

what is a reason that we can't use a initializer constructor when use (new[]) ?

special thanks to Dish and Computergeek01 .



Last edited on
Dunno. Just a language limitation I guess.
Topic archived. No new replies allowed.