2017-07-26 63 views
1

我是個初學者,我嘗試使用SFML庫,但我有一個問題,我的代碼C++/SFML錯誤C2248 NonCopyableOperator

#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(); 
} 

這個程序只是用來移動矩形或圓形用鍵盤或鼠標。 當所有程序都在主函數中時,我沒有任何問題,但是我希望將來使用子程序和類時,我將進行培訓,但是我有以下錯誤! error C2248

搜索後,我認爲問題來自函數'sf :: RenderWindow',但再次初學者我不明白如何得到問題的周圍。

感謝您的幫助! :)

+0

'fenetre'在定義和聲明時具有不同的簽名。我建議你在使用它之前先放置函數定義,如果你不想把它們放在不同的文件中(而不是正確的,如果你想在這裏發佈),那麼在你使用它的時候不太容易出錯。 – Jepessen

回答

0

的問題是@Jepessen提到你基本上SF :: RenderWindow的返回類型在你的聲明,您的參數是指針類型的SF :: RenderWindow的:

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

然後當您定義的功能,您要退回作廢,你的參數是一個參考SF :: RenderWindow的:

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

爲了解決這個問題,使FENETRE無效的聲明的返回類型,因爲你實際上並沒有返回值,並將參數引用到sf :: Rende rWindow:

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

感謝您的幫助我是一個初學者,我迷路了指針哈哈:) –

+0

@AlexisBENOIT沒有問題的人,如果這有助於你,請務必將其標記爲正確的答案,謝謝! – kooldart

相關問題