In my code I want to declare an instance of my custom class like this:
MyClass anInstance;
if(something){
anInstance = MyClass("instantiated like this");
}else{
anInstance = MyClass("not instantiated like that");
}
//use my anInstance object
...
My IDE is flagging the first line where I declare anInstance, it says: No matching constructor for initialization of 'MyClass'
Is there something illegal about this?
MyClassprobably lacks a default constructor. If so, you need to initialize it when you declare it. Something like this: