Search code examples
c++classvisual-studio-2013direct2d

What is the reason for: error C2512: 'D2D1::ColorF' : no appropriate default constructor available


I am upgrading my GDI+ project to Direct2D. When I rewrote my GDI+ class to Direct2D I got following error message during compilation: error C2512: 'D2D1::ColorF' : no appropriate default constructor available

The header file:

#pragma once
#include "stdafx.h"
#include <chrono>
#include <atomic>
#include "d2d1.h"
#pragma comment (lib, "d2d1.lib")

template <class T> void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}

class Direct3D_Draw
{
HWND mhWnd{};
HDC mHDC{};
PAINTSTRUCT mPS;
HBITMAP bmp{};
HGDIOBJ oldBmp{};

D2D1::ColorF frontcolor;
D2D1::ColorF backcolor;
D2D1::ColorF fontcolor;
D2D1::ColorF fontbackcolor;

public:
Direct3D_Draw(const HWND& hWnd, D2D1::ColorF _backcolor = D2D1::ColorF::White, D2D1::ColorF _frontcolor = D2D1::ColorF::Black);
~Direct3D_Draw();
};

The implementation:

#include "stdafx.h"
#include "Direct3D_Draw.h"
#include <random>
#include <iterator>
#include <chrono> //clock_t
//#include <Dwrite.h>
//#pragma comment (lib, "Dwrite.lib")

Direct3D_Draw::Direct3D_Draw(const HWND& hWnd, D2D1::ColorF ibackcolor, D2D1::ColorF ifrontcolor)
{

}

Direct3D_Draw::~Direct3D_Draw()
{
}

It is not a full code, just a simplified reproduction. Visual Studio 2013 sp5 What is wrong?..

Here the screen of IDE highkight in IDE


Solution

  • Any class member which is not explicitly initialized in the initializer list of the containing class will be initialized by the default empty (()) constructor in any case.

    But ColorF doesn't provide a default empty constructor, as documented here, so when the Direct3D_Draw::Direct3D_Draw() wants to add the hidden

    frontcolor = D2D1::ColorF();
    ...
    

    it doesn't find an default constructor for them, you should explicitly initialize them in the constructor, eg:

    Direct3D_Draw::Direct3D_Draw(....) : frontcolor{0.0f,0.0f,0.0f,0.0f}, ...