2012-05-12 55 views
0

我有一个问题,“没有适当的默认构造函数可用”错误,它可能只是一个微小的东西,我失踪。我对C++相当陌生,但我正在慢慢获得它。C++没有合适的默认构造函数可用C2152,但我没有指定任何构造函数?

据我所知,如果不指定任何构造函数,C++将创建一个默认构造函数。但是,即使我没有指定任何构造函数,我也会得到该错误。我已经尝试了Google的解决方案,但其他人要么扩展类错误或有指定构造函数,因此他们得到这个错误。我没有指定任何构造函数,但我仍然得到这个错误。 DirectXGame类在下面。

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; 
}; 

通知。在我创建类的新实例的主方法中,实际的错误被抛出。

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); 

}

我可能只是缺少你必须用C讲究++中的很多小细节之一。

+0

你是C++的新手,但想要制作DirectX游戏?在堆栈上使用对象有问题吗? – chris

+1

DirectXInput :: DirectInputHelper inputHelper应该是一个指针吗? – Darcara

+0

打赌你没有读完整个错误信息。 –

回答

1

您缺少空白/默认构造函数,编译器无法猜测如何自动为您实现一个。

如果它是一个微不足道的(非复杂的)类,C++将为您创建一个空构造函数。如果你的班级来自另一个班级,那么它的祖先必须实施默认的构造函数或者本身就是微不足道的。

如果您的类包含其他类,则包含的类必须具有为其定义的默认构造函数,或者它们本身必须很简单,以便编译器也可以为它们实现简单的构造函数。

在你的情况下,罪魁祸首可能是DirectXInput::DirectInputHelper inputHelper;,因为你的类没有任何祖先,并且你的所有数据成员都是指针,基本数据类型或定义的数据类型,而不是那一个类的数据变量。

+0

我将inputHelper更改为一个指针,并解决了这个问题。所以现在我还有其他问题。如果我的类包含基元,指针和结构之外的其他东西,那么将不会生成默认的构造函数,那么我是否正确? –

+0

@NkosiDean:不,不正确。最简单的方法是这样的:对象的编译器生成的默认构造函数将递归地调用其每个子对象的默认构造函数(或者在POD对象的情况下,它将基本上分配它们)。因此,如果你的任何子对象没有默认的构造函数,并且不是POD对象,那么不会为你生成(也不能)默认的构造函数。另外,如果你的类有const对象或引用,则不会生成默认的构造函数,因为这些都需要用某个值初始化。 –

+0

好吧,我想我明白了。感谢您解决这个问题。 :) –

相关问题