Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Non-Copyable error that keeps arriving!  (Read 1884 times)

0 Members and 1 Guest are viewing this topic.

bryce910

  • Newbie
  • *
  • Posts: 31
    • View Profile
Non-Copyable error that keeps arriving!
« 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!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Non-Copyable error that keeps arriving!
« Reply #1 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
}
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Non-Copyable error that keeps arriving!
« Reply #2 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>());
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: