简体   繁体   English

C ++ / SFML错误C2248 NonCopyableOperator

[英]C++ / SFML error C2248 NonCopyableOperator

I'm a beginner and I try to use the SFML library but I have a problem with my code 我是一个初学者,我尝试使用SFML库,但是我的代码有问题

#pragma region include
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
#pragma endregion include


sf::RenderWindow fenetre(sf::RenderWindow*);
sf::RectangleShape rectDraw(sf::RectangleShape*);
sf::CircleShape cercleDraw(sf::CircleShape*);
void CentreCercle(sf::CircleShape *cercle, int*, int*);
void fermetureFenetre();

void main()
{
#pragma region variables
sf::RenderWindow window; 
fenetre(&window);

sf::RectangleShape rect; 
rectDraw(&rect);;

sf::CircleShape cercle; 
cercleDraw(&cercle);

int xCentreCercle;
int yCentreCercle;
CentreCercle(&cercle, &xCentreCercle, &yCentreCercle);

float speed = 1;

sf::Vector2i positionSouris;
#pragma endregion variables

while(window.isOpen())
{
#pragma region fenOpen
    sf::Event event;        
    while(window.pollEvent(event))
    {
        if(event.type == sf::Event::Closed)
            window.close();
    }
#pragma endregion fenOpen

#pragma region KeyboardRectangle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    rect.move(0, -speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    rect.move(0, speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    rect.move(-speed, 0);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))   
    rect.move(speed, 0);
#pragma endregion KeyboardRectangle

#pragma region KeyboardCercle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
    cercle.move(0, -speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    cercle.move(0, speed);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
    cercle.move(-speed, 0);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    cercle.move(speed, 0);
#pragma endregion KeyboardCercle

#pragma region MouseCercle
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        bool boucle = true;
        sf::Vector2f position = cercle.getPosition();
        xCentreCercle = cercle.getPosition().x + cercle.getRadius();
        yCentreCercle = cercle.getPosition().y+ cercle.getRadius();

        float rayon = cercle.getRadius();

        positionSouris = sf::Mouse::getPosition(window);
        int xSouris = positionSouris.x;
        int ySouris = positionSouris.y;

        float resultat = (xCentreCercle-xSouris)*(xCentreCercle-xSouris)+(yCentreCercle-ySouris)*(yCentreCercle-ySouris);



        if(sqrt(resultat)<= rayon)
        {
            int diffX;
            int diffY;      

            diffX = xSouris-cercle.getPosition().x;
            diffY = ySouris-cercle.getPosition().y;

            while(boucle == true)
            {
                positionSouris = sf::Mouse::getPosition(window);
                xSouris = positionSouris.x;
                ySouris = positionSouris.y;


                cercle.setPosition(xSouris - diffX, ySouris - diffY);

                window.draw(rect);
                window.draw(cercle);
                window.display();
                window.clear();

                if (sf::Mouse::isButtonPressed(sf::Mouse::Left) == false)
                    boucle = false;
            }
        }
    }
    if(sf::Mouse::isButtonPressed(sf::Mouse::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::C))
    {
        positionSouris = sf::Mouse::getPosition(window);
        int xSouris = positionSouris.x;
        int ySouris = positionSouris.y;
        cercle.setPosition(xSouris, ySouris);
    }
    #pragma endregion MouseCercle

#pragma region Mouserectangle
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    bool boucle = true;
    sf::Vector2f position = rect.getPosition();
    sf::Vector2f taille = rect.getSize();

    int xMax = position.x + taille.x;
    int xMin = position.x;
    int yMax = position.y + taille.y;
    int yMin = position.y;

    positionSouris = sf::Mouse::getPosition(window);
    int xSouris = positionSouris.x;
    int ySouris = positionSouris.y;

    if(xSouris > xMin && xSouris < xMax && ySouris > yMin && ySouris < yMax)
    {
        int diffX;
        int diffY;

        diffX = xSouris-xMin;
        diffY = ySouris-yMin;

        while(boucle == true)
        {
            positionSouris = sf::Mouse::getPosition(window);
            xSouris = positionSouris.x;
            ySouris = positionSouris.y;

            rect.setPosition(xSouris - diffX, ySouris - diffY);
            window.draw(rect);
            window.draw(cercle);
            window.display();
            window.clear();

            if (sf::Mouse::isButtonPressed(sf::Mouse::Left) == false)
                boucle = false;
        }
    }
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Right)&& sf::Keyboard::isKeyPressed(sf::Keyboard::R))
{
    positionSouris = sf::Mouse::getPosition(window);
    int xSouris = positionSouris.x;
    int ySouris = positionSouris.y;
    rect.setPosition(xSouris, ySouris);
}
#pragma endregion MouseRectangle

    window.draw(rect);
    window.draw(cercle);
    window.display();
    window.clear();
}
}

void fenetre(sf::RenderWindow &window)
{
window.create(sf::VideoMode(800,600),"Tuto SFML");
window.setPosition(sf::Vector2i(50,50));
window.setFramerateLimit(60);
}

void fermetureFenetre(sf::RenderWindow &window)
{
    sf::Event event;        
    while(window.pollEvent(event))
    {
        if(event.type == sf::Event::Closed)
            window.close();
    }
}

void rectDraw(sf::RectangleShape &rect)
{
rect.setSize(sf::Vector2f(200, 100));
rect.setPosition(10, 10);
rect.setFillColor(sf::Color(250, 100, 150));

}

void cercleDraw(sf::CircleShape &cercle)
{
cercle.setFillColor(sf::Color(100,250,50));
cercle.setRadius(50);
cercle.setPosition(0, 0);
}

void CentreCercle(sf::CircleShape &cercle, int &xCentreCercle, int 
&yCentreCercle)
{
xCentreCercle = cercle.getPosition().x + cercle.getRadius();
yCentreCercle = cercle.getPosition().y+ cercle.getRadius();
}

This program just serve to move rectangle or circle with keyboard or mouse. 该程序仅用于用键盘或鼠标移动矩形或圆形。 When all the program is in the main function I don't have any probleme but I want use subroutine and class in the future when I will be training but I have the following error ! 当所有程序都位于主函数中时,我没有任何问题,但是我想在将来训练时使用子例程和类,但是有以下错误! error C2248 错误C2248

After search I think the probleme come from function 'sf::RenderWindow' but again beginner I don't understand how get arround the problem. 搜索后,我认为问题出自“ sf :: RenderWindow”函数,但对于初学者而言,我也不知道如何解决该问题。

Thanks to help! 多谢帮助! :) :)

The problem is as @Jepessen mentioned you basically have a return type of sf::RenderWindow on your declaration, and your parameter is of type Pointer sf::RenderWindow: 问题是,就像@Jepessen提到的,声明中的返回类型基本上是sf :: RenderWindow,而参数的类型是Pointer sf :: RenderWindow:

sf::RenderWindow fenetre(sf::RenderWindow*);

And then when you define the function, you are returning void, and your parameter is a Reference sf::RenderWindow: 然后,当您定义函数时,您将返回void,并且您的参数是Reference sf :: RenderWindow:

void fenetre(sf::RenderWindow &window)
{
   // Code
}

To Fix this, make the return type of the declaration of fenetre void, since you are not actually returning a value at all, and make your parameter Reference to sf::RenderWindow: 要解决此问题,请使fenetre声明的返回类型为空,因为您实际上根本没有返回任何值,然后将您的参数引用为sf :: RenderWindow:

// Declaration, on top of your program
    void fenetre(sf::RenderWindow&);
// Definition, at the bottom
    void fenetre(sf::RenderWindow &window)
    {
       // Code
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM