Reputation: 327
I am having a problem with that "No appropriate default constructor available" error, and it is probably just ONE TINY thing that I am missing. I'm pretty new to C++, but I'm slowly getting it.
As I understand, C++ will create a default constructor if you don't specify any constructors. However, I get the error even though I have not specified any constructors. I've tried googling solutions to this, but everyone else is either extending a class wrong or HAVE specified constructors, hence they get this error. I HAVE NOT specified any constructors, yet I get this error anyway. The DirectXGame class is below.
DirectXGame.h
#include "StdAfx.h"
#include "DirectInputHelper.h"
class DirectXGame
{
public:
//DirectXGame();
bool Initialize(HINSTANCE hInstance, HWND windowHandle);
void ShutDown();
bool LoadContent();
void UnloadContent();
void Update(float timeDelta);
void Render();
private:
HINSTANCE progInstance;
HWND winHandle;
D3D_DRIVER_TYPE driverType;
D3D_FEATURE_LEVEL featureLevel;
ID3D11Device* pDev;
ID3D11DeviceContext* pDevContext;
ID3D11RenderTargetView* pBackBufferTarget;
IDXGISwapChain* pSwapChain;
DirectXInput::DirectInputHelper inputHelper;
DirectXInput::KeyboardState* keyboardDevice;
DirectXInput::MouseState* mouseDevice;
bool isShutDown;
};
Notice I have not specified any constructor, as they've been commented out. The actual error is thrown on the line in the main method where I create a new instance of the class.
DirectXApp.cpp
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
UNREFERENCED_PARAMETER( hPrevInstance );
UNREFERENCED_PARAMETER( lpCmdLine );
if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
return 0;
//std::auto_ptr<DirectXGame> DirectXGame( new DirectXGame() );
DirectXGame* game = new DirectXGame(); //The compile error is on this line.
bool result = game->Initialize(g_hInst, g_hWnd);
if(!result)
{
game->ShutDown();
return -1;
}
// Main message loop
MSG msg = {0};
while(TRUE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message == WM_QUIT)
break;
}
else
{
game->Update(0.0f);
game->Render();
}
}
game->ShutDown();
//Don't forget to delete the game object, as it is a pointer.
delete game;
return static_cast<int>(msg.wParam);
}
I'm probably just missing one of the many small details you have to pay attention to in C++.
Upvotes: 0
Views: 639
Reputation: 2272
You are missing your blank/default constructor, and the compiler cannot guess at how to implement one for you automatically.
C++ will create a blank constructor for you if it is a trivial (non-complex) class. If your class is descended from another class, then its ancestory must implement default constructors or be trivial in themselves.
If your class includes other classes, the included classes must either have default constructors defined for them, or they must be trivial in themselves so that the compiler can implement trivial constructors for them as well.
In your case, the culprit is likely DirectXInput::DirectInputHelper inputHelper;
as your class does not have any ancestors, and all your data members are pointers, basic data types, or defined data types other than that one class data variable.
Upvotes: 2