SFML community forums

Help => Audio => Topic started by: bryce910 on October 04, 2015, 04:01:22 am

Title: Non-Copyable error that keeps arriving!
Post by: bryce910 on October 04, 2015, 04:01:22 am
Hey guys!

I am having an issue with telling the vector music file to play.. I keep getting these errors:


>C:\SFML-2.3\include\SFML/Audio/InputSoundFile.hpp(203): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          C:\SFML-2.3\include\SFML/System/NonCopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          C:\SFML-2.3\include\SFML/System/NonCopyable.hpp(42) : see declaration of 'sf::NonCopyable'



int rulesCounter = 0;  
for (std::vector<sf::Music>::iterator mitr = v_musicList.begin(); mitr != v_musicList.end(); mitr++)
        {
                                v_musicList[rulesCounter].play();
rulesCounter++;
        }

 

If anyone could explain to me what I am doing wrong that would be awesome!
Title: Re: Non-Copyable error that keeps arriving!
Post by: zsbzsb on October 04, 2015, 04:13:54 am
sf::Music is noncopyable, hence the error. This means you can not make copies of the class, so the following code will not work...

sf::Music myMusic;
sf::Music myMusicCopy = myMusic;

Now std::vector<T> requires that its elements be copyable because when it resizes it allocates another array and copies all the previous elements into it. So to solve your problem you need to store pointers in your vector. So use something like the following...

std::vector<std::unique_ptr<sf::Music>> myMusicList;
myMusicList.push_back(std::unique_ptr<sf::Music>(new sf::Music));

for (auto& music : myMusicList)
{
  // do whatever you want when looping
}
Title: Re: Non-Copyable error that keeps arriving!
Post by: Nexus on October 04, 2015, 04:53:05 pm
Don't use iterators and indices for iteration... Just go for range-based for loops as shown by zsbzsb.

If your compiler supports C++14, you can also use std::make_unique() for construction:
myMusicList.push_back(std::make_unique<sf::Music>());