2016-07-25 30 views
1

當我嘗試編譯下面的代碼:C++編譯SFML錯誤SF ::不可複製::不可複製(常量SF ::不可複製&)是私人

SFMLSet.cpp:

#include "SFMLSet.h" 

SFMLSet::SFMLSet(string texturePath) 
{ 
    if(!texture.loadFromFile(texturePath)) { 
     exit(1); 
    } 
    new (&app) sf::RenderWindow(sf::VideoMode(texture.getSize().x, texture.getSize().y), texturePath, sf::Style::None); 
    new (&sprite) sf::Sprite(texture); 
} 

SFMLSet.h :

#ifndef SFMLSET_H 
#define SFMLSET_H 
#include <SFML/Graphics.hpp> 
#include <string> 
#include <cmath> 
using namespace std; 

class SFMLSet { 
    public: 
     sf::RenderWindow app; 
     sf::Texture texture; 
     sf::Sprite sprite; 

     sf::Vector2i grabbedOffset; 
     bool grabbedWindow = false; 

     SFMLSet (string texturePath); 

     sf::Event event; 
}; 


#endif // SFMLSET_H 

main.cpp中:

#include <windows.h> 
#include <vector> 
#include <iostream> 

#include "SFMLSet.h" 


int main() 
{ 
    bool isRunning=true; 
    vector<SFMLSet> IMGS; 
    IMGS.push_back (SFMLSet ("cb.bmp")); 

    while (isRunning) 
    { 
     for (int i=0;i<IMGS.size();i++) { 
     while (IMGS[i].app.pollEvent(IMGS[i].event)) 
     { 
      if (IMGS[i].event.type == sf::Event::Closed) { 
       IMGS[i].app.close(); 
       isRunning=false; 
      } 
      if (IMGS[i].event.type == sf::Event::KeyPressed && IMGS[i].event.key.code == sf::Keyboard::Escape) 
         { 
           IMGS[i].app.close(); 
           isRunning=false; 
         } 
         else if (IMGS[i].event.type == sf::Event::MouseButtonPressed) 
         { 
           if (IMGS[i].event.mouseButton.button == sf::Mouse::Left) 
           { 
             IMGS[i].grabbedOffset = IMGS[i].app.getPosition() - sf::Mouse::getPosition(); 
             IMGS[i].grabbedWindow = true; 
           } 
         } 
         else if (IMGS[i].event.type == sf::Event::MouseButtonReleased) 
         { 
           if (IMGS[i].event.mouseButton.button == sf::Mouse::Left) 
             IMGS[i].grabbedWindow = false; 
         } 
         else if (IMGS[i].event.type == sf::Event::MouseMoved) 
         { 
           if (IMGS[i].grabbedWindow&&(IMGS[i].grabbedOffset.x<-10&&IMGS[i].grabbedOffset.y<-10)&&(IMGS[i].grabbedOffset.x>-(IMGS[i].texture.getSize().x)+10&&IMGS[i].grabbedOffset.y>-(IMGS[i].texture.getSize().y)+10)) 
             IMGS[i].app.setPosition(sf::Mouse::getPosition() + IMGS[i].grabbedOffset); 
         } 
     } 

     IMGS[i].app.clear(); 

     IMGS[i].app.draw(IMGS[i].sprite); 

     IMGS[i].app.display(); 
     } 
    } 

    return EXIT_SUCCESS; 
} 

我收到一些錯誤:

SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private

SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: error: within this context

SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private

SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25: error: within this context

SFML-2.3.2\include/SFML/System/NonCopyable.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)' is private

SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: error: within this context

SFML-2.3.2\include/SFML/System/NonCopyable.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)' is private

SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25: error: within this context

如何解決這個問題?

+1

我不想聽起來粗魯,但從正確學習C++的基礎開始。例如,'new(&app)sf :: RenderWindow(...'是**不是**你應該在這裏做什麼。 – Hiura

回答

4

您應該閱讀SFML tutorials,並編寫像他們的例子一樣的程序。

這裏的具體問題是sf::RenderWindow的拷貝構造函數是私有的 - 通常複製一個窗口是沒有意義的。

不幸的是,您在std::vector中使用了SFMLSet。矢量必須動態增大其大小,爲了實現這一點,他們分配一個新的更大的緩衝區,並將其現有內容複製到新位置 - 調用SFMLSet的複製構造函數,該構造函數依次嘗試調用sf::RenderWindow s。

解決此問題的最佳方法是從IMGS中刪除sf::RenderWindow,並將其作爲局部變量保存在main中,如同在教程中一樣。你可能不打算爲每個圖像打開一個新窗口,對吧?

+0

如果你使用移動類型,你不能使用'std :: vector' 'emplace_back'而不是'push_back'?但我同意使用'sf :: RenderWindow'有什麼問題:'app'應該是一個指向窗口的智能指針 – KABoissonneault

+0

sf :: RenderWindow不可移動 - 它聲明瞭一個私有拷貝構造函數,但是沒有移動構造函數,C++ 11支持在他們的路線圖上,但還沒有實現。 – Dutow

+0

然後它將被動態分配,我猜 – KABoissonneault

2

該錯誤消息告訴您,您正試圖在某處複製sf::NonCopyable的實例。編譯器說你正試圖調用該類的複製構造函數,但該複製構造函數是私人定義的,因此無法訪問。

要修復它,您需要計算出導致sc::NonCopyable實例被複制的原因,並更改該代碼,使其沒有副本(可能使用指針)。