无法访问类 sf::不可复制中的私有成员.副本在哪里?C++
Cannot access private member in class sf::NonCopyable. Where is the copy? C++
使用 SFML 2.1
我决定制作一个包含我的音频文件的对象。但是,当我运行测试时,出现以下错误:
错误 C2248:"sf::不可复制::不可复制":无法访问类"sf::不可复制"中声明的私有成员
我在SFML网站上查找了一些有关此错误的信息,它指出我无法复制继承sf::NonCopyable的对象(我认为)。这些对象可能是我即将展示的sf::Sound和sf::音乐。真正的问题是:这个副本是在哪里制作的,我该如何修复它?
音频类
struct Audio {
std::map<std::string, sf::Sound> sounds;
std::map<std::string, sf::Music> musics;
Audio() : sounds(),
musics(){}
void addSound(sf::Sound& s, sf::SoundBuffer& sb, std::string key){
s.setBuffer(sb);
sounds.insert(std::pair<std::string, sf::Sound>(key, s));
}
void addSound(sf::Sound& s, std::string key){
sounds.insert(std::pair<std::string, sf::Sound>(key, s));
}
void addMusic(sf::Music& m, std::string key){
musics.insert(std::pair<std::string, sf::Music>(key, m));
}
sf::Sound& getSound(std::string key){
return sounds[key];
}
sf::Music& getMusic(std::string key){
return musics[key];
}
};
加载声音方法
void loadSounds(Audio& audio){
sf::Music backgroundMusic;
sf::Sound eating;
sf::SoundBuffer sb_eating;
sf::Sound moving;
sf::SoundBuffer sb_moving;
sf::Sound losing;
sf::SoundBuffer sb_losing;
sf::Sound begin;
sf::SoundBuffer sb_begin;
if (!backgroundMusic.openFromFile("backgroundmusic.wav"))
std::cerr << "Error opening "backgroundmusic.wav"" << std::endl;
if (!sb_eating.loadFromFile("eatingsfx.wav"))
std::cerr << "Error opening "eatingsfx.wav"" << std::endl;
if (!sb_moving.loadFromFile("movingsfx.wav"))
std::cerr << "Error opening "movingsfx.wav"" << std::endl;
if (!sb_losing.loadFromFile("losingsfx.wav"))
std::cerr << "Error opening "losingsfx.wav"" << std::endl;
if (!sb_begin.loadFromFile("beginsfx.wav"))
std::cerr << "Error opening "beginsfx.wav"" << std::endl;
eating.setBuffer(sb_eating);
moving.setBuffer(sb_moving);
losing.setBuffer(sb_losing);
begin.setBuffer(sb_begin);
audio.addMusic(backgroundMusic, std::string("backgroundMusic"));
audio.addSound(eating, std::string("eating"));
audio.addSound(moving, std::string("moving"));
audio.addSound(losing, std::string("losing"));
audio.addSound(begin, std::string("begin"));
}
主要方法
int main(){
/*Initialize the objects*/
Snake snake = Snake();
Audio& audios = Audio();
sf::Text textCount;
Apple apple(0, 0);
apple.locateApple();
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "SFML Application" );
/*Load the audio*/
loadSounds(audios);
audios.getMusic("backgroundMusic").setVolume(10);
audios.getMusic("backgroundMusic").setLoop(true);
audios.getMusic("backgroundMusic").setVolume(25);
/*Load the font*/
sf::Font font;
if (!(font.loadFromFile("arial.ttf")))
std::cout << "Error loading fonts" << std::endl;
/*Create the text*/
textCount.setFont(font);
textCount.setString(std::string("points: ") + std::to_string(points));
textCount.setColor(sf::Color::Red);
textCount.setCharacterSize(20);
textCount.setPosition(windowWidth / 2 - (textCount.getString().getSize()*(textCount.getCharacterSize() / 5)), textCount.getCharacterSize() - 5);
textCount.setStyle(sf::Text::Bold);
window.draw(textCount);
/*Set Framerate fps*/
window.setFramerateLimit(10);
/*MAIN GAME LOOP*/
counterTick = 1;
audios.getSound("begin").play();
audios.getMusic("backgroundMusic").play();
while (inGame)
{
std::string counter = std::to_string(counterTick);
std::cout << "Tick: " + counter << std::endl;
window.clear(sf::Color::Black);
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) break;
/*Call Updates*/
snake.input();
snake.checkReals();
snake.moveUpdate();
audios.getSound("moving").play();
/*Call Collisions*/
std::cout << " Outside Collision Loop " << std::endl;
checkApple(snake, apple, audios.getSound("eating"));
checkBoundary(snake);
/*Call Draw*/
std::vector<sf::RectangleShape> shapearray = snake.draw();
for (int i = shapearray.size() - 1; i >= 0; i--){
window.draw(shapearray[i]);
}
window.draw(textCount);
window.draw(apple.draw());
window.display();
counterTick++;
}
audios.getSound("losing").play();
audios.getMusic("backgroundMusic").stop();
std::system("PAUSE");//bad practice, debuggin purposes
return 0;
}
就像已经提到的评论一样,sf::Music
无法复制。您最好的选择是将其包装在std::unique_ptr
中,因为您不需要共享所有权。
既然你在前面的问题中说过你这样做是为了学校,我将向你展示如何使用 C++11 来做到这一点
#include <string>
#include <map>
#include <memory>
//non copyable and non movable like sf::Music
struct NonCopyable
{
NonCopyable() {};
NonCopyable(const NonCopyable &&other) = delete;
NonCopyable(const NonCopyable &other) = delete;
void randomFunc(){}
};
struct MapHolder
{
std::map<std::string, std::unique_ptr<NonCopyable>> container;
void addElement(const std::string &key, std::unique_ptr<NonCopyable> value)
{
//std::move is necessary here, compiler error without it, since
//unique_ptr<T> is non-copyable but can be moved just fine
container.insert({key , std::move(value)});
}
NonCopyable &get(const std::string &key)
{
//using container[key] will create a new NonCopyable if none is found
//this will however throw an exception if key is not in the map
return *container.at(key);
}
//probably not necessary in this case but this is so that you can get a
//const NonCopyable elements from a "const MapHolder", without this,
//calling ".get" on a "const MapHolder" would be an error
const NonCopyable &get(const std::string &key) const
{
return *container.at(key);
}
};
int main()
{
MapHolder holder;
auto myInstance = std::make_unique<NonCopyable>();
//if make_unique is not supported on your compiler use this:
//auto myInstance = std::unique_ptr<NonCopyable>(new NonCopyable());
//this is how you access members of NonCopyable
myInstance->randomFunc();
//again, the move here is necessary, since std::unique_ptr is non-copyable
//after this line "myInstance" is invalid
holder.addElement("test", std::move(myInstance));
//doing this is illegal now, since myInstance is now moved
//it should be noted that std::move was only a cast, the actual moving
//happened because "addElement" took the unique_ptr by value and
//thereby transferred the ownership during construction of its local parameter
//myInstance->randomFunc();
//this is ok
holder.get("test").randomFunc();
//this is fine too
NonCopyable &local_ref = holder.get("test");
local_ref.randomFunc();
return(0);
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在模板类中返回成员变量的副本
- 在实例副本之间共享的类成员
- 保留对象成员变量的本地副本
- 是否有一种方法可以始终通过值(制作副本)而不是在使用类成员函数时通过参考来传递
- 如何为同一类对象的成员函数保留单独的变量副本?
- 混合共享/静态库时静态成员的多个副本
- 设计一个具有大型成员的C++类,而这些成员很少在副本中访问
- 您应该将自动副本移到成员中还是将其保留
- 如何在基类中获取受保护成员的"真实"副本
- 无法访问类 sf::不可复制中的私有成员.副本在哪里?C++
- 删除结构的成员,并将其重新制作为其他内容的副本,而不使用 new
- 如何避免C++派生类中基类成员的重复副本
- C++类:在成员函数中创建"this"类的副本
- 使用已删除的副本构造函数和初始值设定项列表重载调用类定义中的成员构造函数
- C++通过对构造函数的引用传递.成员将是一个完整的私人副本
- 使复制的成员引用变量引用副本的成员,而不是原始成员的成员
- 在c++中通过成员函数返回引用比返回副本快
- 非静态成员函数,为每个对象创建生成一份机器码副本
- 赋值运算符-每个成员或二进制副本