Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试使用osstream将int转换为字符串时出现错误C2280_C++_String_Drawtext_Ostringstream - Fatal编程技术网

C++ 尝试使用osstream将int转换为字符串时出现错误C2280

C++ 尝试使用osstream将int转换为字符串时出现错误C2280,c++,string,drawtext,ostringstream,C++,String,Drawtext,Ostringstream,我只是尝试使用DrawText输出一个整数变量 以下是错误: 错误7错误C2280: 'std::basic_ostringstream,std::allocator>::basic_ostringstreamconst std::基本流,std::分配器> &“:尝试引用已删除的函数 以下是基本代码: //Domino.h #pragma once #include <Windows.h> #include <string> #inclu

我只是尝试使用DrawText输出一个整数变量

以下是错误:

错误7错误C2280: 'std::basic_ostringstream,std::allocator>::basic_ostringstreamconst std::基本流,std::分配器> &“:尝试引用已删除的函数

以下是基本代码:

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

    }
你有两个问题:

您将所有函数声明为接受按值传递的参数,因此传递的每个参数在传递给函数之前都会被复制,请参阅。无论如何都应该避免这种情况,因为这是一种浪费内存的行为,而且比通过引用传递要慢,但在这种特殊情况下,它也会导致。。。 当您将Domino对象传递给函数时,它会被复制。但是您的类是不可复制的,因为他的一个成员myVar是不可复制的:他的复制构造函数已禁用,因此尝试引用已删除的函数时出错。
因此,解决方案是:习惯按引用传递并重新声明所有函数以接受对对象的引用。

IO流无法复制。除非您定义复制行为,否则复制Domino不会比这更聪明。谢谢!我不知道你是什么意思。。。你能进一步解释一下吗?你要么做一个复制构造函数和赋值运算符,它做的事情比简单地复制字符串流更有用,这可能很难考虑到不应该有流的副本四处浮动,要么找到一种不让普通字符串流作为成员的方法,或者不要对Domino做任何需要复制或赋值的事情。哇。。。好的,谢谢:我真的不知道我是怎么复制的,但是。。我只需要使用DrawText显示一个变量!这是非常复杂的:假设这是C++03,通过值复制和向量复制元素。这是一个开始。