简体   繁体   中英

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

#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

After search I think the probleme come from function 'sf::RenderWindow' but again beginner I don't understand how get arround the problem.

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:

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 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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