0

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);
} 
  • Since this question about confusing pointers and references, and displays some anti-patterns, it might be best answered with a good C++ book. Perhaps you should have a look at the [C++ recommended book list](http://stackoverflow.com/questions/388242/). – jaggedSpire Jan 26 '17 at 19:07

2 Answers2

1

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;
}
Sam Marinelli
  • 999
  • 1
  • 6
  • 16
0

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"){}

yahoo5000
  • 470
  • 6
  • 18