问题
I have a class called MainWindow
without a default constructor. I have a class called Application
, its constructor uses an instance of MainWindow
as a parameter. I get an
Error C2512, "no appropriate default constructor available"
in the definition of the constructor from the class Application.
Here's the code of the constructor:
Application::Application(HINSTANCE hInstance, MainWindow mainWindow) {...}
I'm creating the instance of Application
like this:
MainWindow window(1000, 1000, false, "test");
Application program(Instance, window);
Why do I get this error? I'm not trying to create a new instance of MainWindow with the default constructor.
回答1:
It seems that class Application has a data member of type MainWindow, that can not be created. Otherwise it is not clear why the constructor of class Application has parameter MainWindow mainWindow
. I think it is used that to assign it to the data member of the class.
You could use the ctor-initializer. For example if class Application indeed has a data member of type MainWindow (let name it m_window) then you could write the constructor the following way
Application::Application(HINSTANCE hInstance, MainWindow mainWindow) : m_window( mainWindow )
{
//...
}
回答2:
If the problem is no a MainWindow class member then your Application class has a constructor which takes a MainWindow parameter by value. In order to do this it must create a copy of window, if you have no copy constructor, the compiler may be looking for some way to create this copy. Try:
Application::Application(HINSTANCE hInstance, MainWindow &mainWindow) {...}
来源:https://stackoverflow.com/questions/23028691/c-error-c2512-no-appropriate-default-constructor-available