tonero gnwrizei
tonero gnwrizei

Reputation: 11

how can i initialize an object

The point is to initialize a pointer to a Mystic object with the value "beep" any idea?

class Mystic { 
private:
 string label;
 Mystic(string & newlbl) { setLabel (newlbl)};
public:
 void setLabel(string newlbl){label = newlbl;}
 Mystic() : label(){};
};
int main(int argc, char *argv[])
{
...    //i tried this
       //string *p1 = new string("beep");
      //Mystic myst(p1);
} 

Upvotes: 0

Views: 73

Answers (2)

yahoo5000
yahoo5000

Reputation: 458

Constructor you trying to use its a private , and you can only access public one , so you have to make that constructor public , or if you want to use public default constructor and initialize default value Mystic() : label("default"){}

Upvotes: 0

Sam Marinelli
Sam Marinelli

Reputation: 1119

The constructor that takes a string is not public, so you can't use it. Instead use the default constructor and then the setLabel method.

int main(int argc, char** argv) {
  Mystic m;
  m.setLabel("beep");
  Mystic* p = &m;
}

Upvotes: 1

Related Questions