-1

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

Märs
  • 37
  • 5
  • 1
    Try constructing the member colors in an initializer list in your `Direct3D_Draw`'s constructor. They do not provide a default constructor and require values when your `Direct3D_Draw` class is constructed. – E. Moffat Nov 16 '15 at 19:38
  • `What is wrong?` Doesn't the error tells you what's wrong? Obviously you can't create `D2D1::ColorRef` objects with no arguments due to that constructor not being available. – PaulMcKenzie Nov 16 '15 at 19:42

1 Answers1

1

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}, ...
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Thank you! BMPD2DGenerator::BMPD2DGenerator(const HWND& hWnd, D2D1::ColorF _backcolor, D2D1::ColorF _frontcolor): frontcolor(_frontcolor), fontcolor(_frontcolor), backcolor(_backcolor), fontbackcolor(_backcolor) {...... – Märs Nov 16 '15 at 19:51