#SFML sf::Music causes a compiler error due to being inside a struct.

38 messages · Page 1 of 1 (latest)

rocky root
#

I'm having trouble with my compiler giving the following error:

539 28 C:\mingw32\include\c++\13.1.0\bits\alloc_traits.h [Error] no matching function for call to 'construct_at(MUSIC*&, MUSIC)'

It deals with an sf::Music object being in a vector of a struct and trying to add new elements of that struct to the vector.

Here is the code:



struct MUSIC
{
    sf::Music music;
    std::string path = "";
    
};


namespace ROD 
{

    std::vector<MUSIC> music;

    int CreateMusic(std::string path = "NULL")
    {
        for(int a=0;a<music.size();a++)
        {
            if(music[a].path == path)
                return a;
        }
        
        if(path == "NULL")
            return music.size() - 1;
        
        
        MUSIC newMusic;
        if(!newMusic.music.openFromFile(path))
        {
            return -1;    
        }
        newMusic.path = path;
        music.push_back(std::move(newMusic));        
            
        return music.size() - 1;
    }
}

Any ideas why this is a problem and how to fix it?

tame yew
rocky root
#

That's what I thought. Though originally I had used emplace_back() and got the following error:

90 56 C:\mingw32\include\c++\13.1.0\bits\stl_uninitialized.h [Error] static assertion failed: result type must be constructible from input type

And it showed a line static_assert in this code snippet from stl_uninitialized.h.

__check_constructible()
{
  // Trivial types can have deleted constructors, but std::copy etc.
  // only use assignment (or memmove) not construction, so we need an
  // explicit check that construction from _Tp is actually valid,
  // otherwise some ill-formed uses of std::uninitialized_xxx would
  // compile without errors. This gives a nice clear error message.
  static_assert(is_constructible<_ValueType, _Tp>::value,
  "result type must be constructible from input type");

  return true;
}

The full error for push_back is similiar:

539 28 C:\mingw32\include\c++\13.1.0\bits\alloc_traits.h [Error] no matching function for call to 'construct_at(MUSIC*&, MUSIC)'

construct(allocator_type& __a __attribute__((__unused__)), _Up* __p,
      _Args&&... __args)
noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
{

#if __cplusplus <= 201703L
__a.construct(__p, std::forward<_Args>(__args)...);
#else
std::construct_at(__p, std::forward<_Args>(__args)...);
#endif
}

  /**
   *  @brief  Destroy an object of type @a _Up
   *  @param  __a  An allocator.
   *  @param  __p  Pointer to the object to destroy
tame yew
rocky root
#

Heres the code for the emplace_back method:

int CreateMusic(std::string path = "NULL")
{

    for(int a=0;a<music.size();a++)
    {
        if(music[a].path == path)
            return a;
    }        
    
    music.emplace_back();
    
    if(path == "NULL")
        return music.size() - 1;
    
    if(!music.back().music.openFromFile(path))
    {
        music.pop_back();
        return -1;    
    }    
    music.back().path = path;
    return music.size() - 1;
}
rocky root
#

That is the full error

tame yew
#

I doubt that

rocky root
#

I guess there is more for other errors.

tame yew
#

Usually it gives you the full stack of context for this kind of error

#

Which line is that error for?

rocky root
#

For this line:

static_assert(is_constructible<_ValueType, _Tp>::value,

And this line:

music.emplace_back();

tame yew
#

That file is unreadable

#

Put it on Pastebin or something

#

What version of CPP are you using?

rocky root
#

Not sure what you want. Whats the difference between pastebin and this?

tame yew
rocky root
tame yew
#

In general you should always share bigger snippets using a service like Pastebin, gist. GitHub, hastebin etc

#

Here's what I see for context

rocky root
#

No why did it do that?

tame yew
#

So please share the output the correct way if you'd like some assistance

rocky root
#

I'm trying ot figure out how to use pastebin.

rocky root
#

I'll try to get it put on pastebin tomorrow.

tame yew
#

You just paste your text and click the button

rocky root
#

I'm not signed into pastebin. I don't have an account for that. Or is an account not necessary?

tame yew
#

You don't need an account

rocky root
tame yew
#

What's the code

#

Oh I see

#

That's sfml 2.6

#

Sf::music was not movablr

#

Either update to sfml 3 or wrap it in a unique_ptr

#

Or put it in a map

rocky root
#

Something I don't understand is why this works in an std::vector when alone but not when it's in a struct and made into a vector that way. What's the difference?

tame yew