Question

I am just trying to output an integer variable using DrawText();

Here is the error:

Error 7 error C2280: 'std::basic_ostringstream,std::allocator>::basic_ostringstream(const std::basic_ostringstream,std::allocator> &)' : attempting to reference a deleted function

Here is the basic code:

//Domino.h
    #pragma once
    #include <Windows.h>
    #include <string>
    #include <sstream>
    class Domino
    {
        public:
            Domino(int xc, int yc);
            //~Domino();
            void update();
                //[ other variables....]
            std::ostringstream myVar;
    };

//Domino.cpp

    #include "Domino.h"
    #include <string>
    #include <sstream>

    Domino::Domino(int xcenter, int ycenter)
    {
        width = 50;
        height = 100;
        dotDiameter = 8;

        xc = xcenter;
        yc = ycenter;

        update();
    }

    void Domino::update()
    {
        x1 = xc - (width / 2);
        x2 = xc + (width / 2);
        y1 = yc - (height / 2);
        y2 = yc + (height / 2);

        std::ostringstream myVar("Value of vDomino[0].xc: ");
        myVar << xc;

        //[...other stuf..]
    }

//main.cpp

    #include <windows.h>
    #include <vector>
    #include "Domino.h"
    #include <math.h>
    #include <time.h>
    #include <string>
    #include <sstream>

    const wchar_t g_szClassName[] = L"myWindowClass";

    void drawDominos(HDC, std::vector<Domino>);
    void drawLine(HDC, Domino);
    void drawDots(HDC, Domino, POINT);
    std::vector<int> vRandValues = {1,2,3,4,5,6};
    int nextRandValueIndexToUse = -1;
    int penwidth = 2;
    //Get a random number from 1 to 6
    int randNum;

    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HDC hDC;
        PAINTSTRUCT ps;
        HPEN hPen;
        HBRUSH hBrush;
        HFONT hFont;
        RECT txtRect;

        int fontHeight;
        int numOfDominos = 3;
        int winWidth = 640;
        int winHeight = 480;
        static int width = 50;
        static int height = 100;
        static bool isRectangle = true;
        static int red = 250;
        static int green = 250;
        static int blue = 250;
        static int penWidth = 5;
        static int xc = 150;
        static int yc = 150;

        Domino D1 = Domino(1 * ((winWidth - (numOfDominos * (width))) / (numOfDominos + 1)) + (width / 2), (winHeight - height) / 2 + height / 2);
        Domino D2 = Domino(2 * ((winWidth - (numOfDominos * (width))) / (numOfDominos + 1)) + (3*width / 2), (winHeight - height) / 2 + height / 2);
        Domino D3 = Domino(3 * ((winWidth - (numOfDominos * (width))) / (numOfDominos+1)) + (5*width / 2), (winHeight - height) / 2 + height / 2);

        std::vector<Domino> vDomino;
        vDomino.push_back(D1);
        vDomino.push_back(D2);
        vDomino.push_back(D3);

        red = 1 + rand() % 255;
        switch (msg)
        {
        case WM_PAINT:
            for (int i = 0; i < vDomino.size(); i++)
            {
                vDomino[i].update();
            }
            hDC = BeginPaint(hwnd, &ps);

            //Contour pen
            hPen = CreatePen(PS_SOLID, penwidth, RGB(0,0,0));
            SelectObject(hDC, hPen);

            //Create fill color
            hBrush = CreateSolidBrush(RGB(red, vDomino[1].green, vDomino[1].blue));
            SelectObject(hDC, hBrush);

            //Create fonts
            fontHeight = 40;
            hFont = CreateFont(fontHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Times New Roman");
            SelectObject(hDC, hFont);

            if (isRectangle)
            {
                drawDominos(hDC, vDomino);
            }
            else
                Ellipse(hDC, xc - (width / 2), yc - (height / 2), xc + (width / 2), yc + (height / 2));


            // Draw some text
            txtRect.top = 0+fontHeight;
            txtRect.bottom = 400;
            txtRect.left = winWidth/2-200;
            txtRect.right = winWidth/2+200;
            DrawText(hDC, ((LPTSTR)(vDomino[0].myVar).str().c_str()), -1, &txtRect, DT_CENTER | DT_WORDBREAK);          **//THIS IS WHERE I USE myVar**

            DeleteObject(hBrush);
            DeleteObject(hPen);
            DeleteObject(hFont);
            EndPaint(hwnd, &ps);
            break;
        case WM_CHAR:
            if (wParam == 'r')
            {
                isRectangle = true;
                InvalidateRect(hwnd, NULL, true);
            }
            else if (wParam == 'c')
            {
                red = rand() % 256;
                green = rand() % 256;
                blue = rand() % 256;
                InvalidateRect(hwnd, NULL, true);
            }
            else if (wParam == 'w')
            {
                //penWidth = 1 + rand() % 20;
                InvalidateRect(hwnd, NULL, true);
            }
            break;
        case WM_KEYDOWN:
            if (wParam == VK_SPACE)
            {
                vRandValues.clear();
                for (int i = 0; i < numOfDominos * 2; i++)
                {
                    vRandValues.push_back(1 + rand() % 6);
                }
                InvalidateRect(hwnd, NULL, true);
            }
            /*if (wParam == VK_UP)
            {
                for (int i = 0; i < vDomino.size(); i++)
                {
                    vDomino[i].height *= 1.1;
                    vDomino[i].width *= 1.1;
                }
                //InvalidateRect(hwnd, NULL, true);
            }
            if (wParam == VK_DOWN)
            {
                for (int i = 0; i < vDomino.size(); i++)
                {
                    vDomino[i].height *= 1.0 / 1.1;
                    vDomino[i].width *= 1.0 / 1.1;
                }
                //InvalidateRect(hwnd, NULL, true);
            }*/
            if (wParam == VK_RIGHT)
            {
                for (int i = 0; i < vDomino.size(); i++)
                {
                    vDomino[i].xc += 5;
                }           
                InvalidateRect(hwnd, NULL, true);
            }
            if (wParam == VK_LEFT)
            {
                for (int i = 0; i < vDomino.size(); i++)
                {
                    vDomino[i].xc -= 5;
                }
                InvalidateRect(hwnd, NULL, true);
            }
            if (wParam == VK_UP)
            {
                for (int i = 0; i < vDomino.size(); i++)
                {
                    vDomino[i].yc -= 5;
                }
                InvalidateRect(hwnd, NULL, true);
            }
            if (wParam == VK_DOWN)
            {
                for (int i = 0; i < vDomino.size(); i++)
                {
                    vDomino[i].yc += 5;
                }
                InvalidateRect(hwnd, NULL, true);
            }
            break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;

        //Step 1: Registering the Window Class
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = 0;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

        if (!RegisterClassEx(&wc))
        {
            MessageBox(NULL, L"Window Registration Failed!", L"Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }

        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            L"Nicolas Maltais-Dansereau",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            NULL, NULL, hInstance, NULL);

        if (hwnd == NULL)
        {
            MessageBox(NULL, L"Window Creation Failed!", L"Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }

        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);

        // Step 3: The Message Loop
        while (GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

    void drawLine(HDC hDC, Domino D)
    {
        int x1, x2, y2, y1, penwidth = 2;
        x1 = D.lineX1;
        y1 = D.lineY1;
        x2 = D.lineX2;
        y2 = D.lineY2;
        POINT p[2];
        p[0].x = x1;
        p[0].y = y1;
        p[1].x = x2 - penwidth;
        p[1].y = y2;

        Polyline(hDC, p, 2);
    }

    void drawDominos(HDC hDC, std::vector<Domino> vDomino)
    {
        for (int i = 0; i < vDomino.size(); i++)
        {
            Rectangle(hDC, vDomino[i].x1, vDomino[i].y1, vDomino[i].x2, vDomino[i].y2); 
            drawLine(hDC, vDomino[i]);
            drawDots(hDC, vDomino[i], vDomino[i].bottomCenter);
            drawDots(hDC, vDomino[i], vDomino[i].topCenter);
        }
    }//ERROR C2280 ON THIS LINE!

    void drawDots(HDC hDC, Domino D, POINT p)
    {
        int x1, y1, x2, y2;

        if (nextRandValueIndexToUse < 5)
            nextRandValueIndexToUse++;
        else
            nextRandValueIndexToUse = 0;

        switch (vRandValues[nextRandValueIndexToUse])
        {
        case 1:
            //Define points
            x1 = p.x - D.dotDiameter / 2;
            y1 = p.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
                Ellipse(hDC, x1, y1, x2, y2);
            break;
        case 2:
            //Break center into 2 points and go from there
            POINT p1, p2; 
            //p1 = top right
                p1.x = p.x + D.width/4;
                p1.y = p.y - D.width/4;
            //p2 = bottom left
                p2.x = p.x - D.width/4;
                p2.y = p.y + D.width/4;

            //Define points for p1
            x1 = p1.x - D.dotDiameter / 2;
            y1 = p1.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
                //Draw p1 dot
                Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p2
            x1 = p2.x - D.dotDiameter / 2;
            y1 = p2.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
                //Draw p2 dot
                Ellipse(hDC, x1, y1, x2, y2);
            break;
        case 3:
            //Copy-pasted case 1+2;
            x1 = p.x - D.dotDiameter / 2;
            y1 = p.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            Ellipse(hDC, x1, y1, x2, y2);
            //POINT p1, p2;
            //p1 = top right
            p1.x = p.x + D.width / 4;
            p1.y = p.y - D.width / 4;
            //p2 = bottom left
            p2.x = p.x - D.width / 4;
            p2.y = p.y + D.width / 4;
            //Define points for p1
            x1 = p1.x - D.dotDiameter / 2;
            y1 = p1.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p1 dot
            Ellipse(hDC, x1, y1, x2, y2);
            //Define points for p2
            x1 = p2.x - D.dotDiameter / 2;
            y1 = p2.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p2 dot
            Ellipse(hDC, x1, y1, x2, y2);
            break;
        case 4:
            //Copy-pasted case 2 + added top-left and bottom right points;
            //Break center into 2 points and go from there
            POINT p3, p4;
            //p1 = top right
            p1.x = p.x + D.width / 4;
            p1.y = p.y - D.width / 4;
            //p2 = bottom left
            p2.x = p.x - D.width / 4;
            p2.y = p.y + D.width / 4;
            //p3 = top left
            p3.x = p.x - D.width / 4;
            p3.y = p.y - D.width / 4;
            //p4 = bottom right
            p4.x = p.x + D.width / 4;
            p4.y = p.y + D.width / 4;

            //Define points for p1
            x1 = p1.x - D.dotDiameter / 2;
            y1 = p1.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p1 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p2
            x1 = p2.x - D.dotDiameter / 2;
            y1 = p2.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p2 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p3
            x1 = p3.x - D.dotDiameter / 2;
            y1 = p3.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p3 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p4
            x1 = p4.x - D.dotDiameter / 2;
            y1 = p4.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p4 dot
            Ellipse(hDC, x1, y1, x2, y2);
            break;

        case 5:
            //Copy paste case 1 and case 4
            //Break center into 2 points and go from there
            //POINT p3, p4;
            //p1 = top right
            p1.x = p.x + D.width / 4;
            p1.y = p.y - D.width / 4;
            //p2 = bottom left
            p2.x = p.x - D.width / 4;
            p2.y = p.y + D.width / 4;
            //p3 = top left
            p3.x = p.x - D.width / 4;
            p3.y = p.y - D.width / 4;
            //p4 = bottom right
            p4.x = p.x + D.width / 4;
            p4.y = p.y + D.width / 4;

            //Define points for p1
            x1 = p1.x - D.dotDiameter / 2;
            y1 = p1.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p1 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p2
            x1 = p2.x - D.dotDiameter / 2;
            y1 = p2.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p2 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p3
            x1 = p3.x - D.dotDiameter / 2;
            y1 = p3.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p3 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p4
            x1 = p4.x - D.dotDiameter / 2;
            y1 = p4.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p4 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points
            x1 = p.x - D.dotDiameter / 2;
            y1 = p.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            Ellipse(hDC, x1, y1, x2, y2);
            break;

        case 6:
            //copy case 4 and add 2 new points
            //Break center into 2 points and go from there
            POINT p5, p6;
            //p1 = top right
            p1.x = p.x + D.width / 4;
            p1.y = p.y - D.width / 4;
            //p2 = bottom left
            p2.x = p.x - D.width / 4;
            p2.y = p.y + D.width / 4;
            //p3 = top left
            p3.x = p.x - D.width / 4;
            p3.y = p.y - D.width / 4;
            //p4 = bottom right
            p4.x = p.x + D.width / 4;
            p4.y = p.y + D.width / 4;
            //p5 = center left
            p5.x = p.x - D.width / 4;
            p5.y = p.y;
            //p4 = center right
            p6.x = p.x + D.width / 4;
            p6.y = p.y;

            //Define points for p1
            x1 = p1.x - D.dotDiameter / 2;
            y1 = p1.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p1 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p2
            x1 = p2.x - D.dotDiameter / 2;
            y1 = p2.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p2 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p3
            x1 = p3.x - D.dotDiameter / 2;
            y1 = p3.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p3 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p4
            x1 = p4.x - D.dotDiameter / 2;
            y1 = p4.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p4 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p5
            x1 = p5.x - D.dotDiameter / 2;
            y1 = p5.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p5 dot
            Ellipse(hDC, x1, y1, x2, y2);

            //Define points for p6
            x1 = p6.x - D.dotDiameter / 2;
            y1 = p6.y - D.dotDiameter / 2;
            x2 = x1 + D.dotDiameter;
            y2 = y1 + D.dotDiameter;
            //Draw p6 dot
            Ellipse(hDC, x1, y1, x2, y2);
            break;
        default:
            break;
        }

    }
Was it helpful?

Solution

You got two problems:

  1. You declared all your functions to accept arguments passed by value so every argument you pass is copied before being passed to the function (see this). This should be avoided anyway because it's a waste of memory and it's slower than passing by reference but in this particolar situation, it's also responsible for...
  2. When you pass a Domino object to a function it gets copied. But your class is not copiable because one of his member (myVar) is not copiable: his copy constructor has ben deleted (disabled) hence the error attempting to reference a deleted function.

So the solution is: get used to passing by reference and redeclare all your functions to accept references to objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top