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

Author Topic: Shadermanager [Problem with Noncopyable]  (Read 3200 times)

0 Members and 1 Guest are viewing this topic.

9100eric

  • Newbie
  • *
  • Posts: 2
    • View Profile
Shadermanager [Problem with Noncopyable]
« on: January 07, 2016, 02:53:43 pm »
Hello evryone :D,

I want to make a ShaderManager. I want to use a std::map<std::string,sf::Shader> //string = ID want to make a ShaderManager. I want to use a std::map<std::string,sf::Shader> //string = ID

 when I try to add a Shader to the map, i get this error:
(click to show/hide)

Here's the Header:
(click to show/hide)

And here's the Cpp:
(click to show/hide)

Can Somebody help me?

p.s: I'm from Germany, so please excuse my grammar :)
« Last Edit: January 07, 2016, 04:14:47 pm by Laurent »

grok

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • Email
Re: Shadermanager [Problem with Noncopyable]
« Reply #1 on: January 07, 2016, 05:17:47 pm »
copy-consructor is forbidden for sf::Shader.
try the following:
  const sf::Shader &ShaderManager::getShader(crString name) const {
    //TODO:
  }
 

oh, wait, also, you cannot add shader in the way you presented because it needs a copy to be created, which is forbidden in the current implementation.
« Last Edit: January 07, 2016, 05:20:08 pm by grok »

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: Shadermanager [Problem with Noncopyable]
« Reply #2 on: January 07, 2016, 09:11:41 pm »
The error says it all - The shader can't be copied. Therefore, the following won't work.

void add_Shader(sf::Shader shader, std::string name)
{
    // do whatever you do
}

sf::Shader a_shader;

add_Shader(a_shader, "a name");  // This will not work, because the function is using arguments by value (i.e. copying it)
 

A common solution to these problems is to pass arguments by reference:

void add_Shader(sf::Shader& shader, std::string name)   // Note the '&' sign right after the sf::Shader
{
    // do whatever you do - but a map needs to hold a shader reference too.
    a_map.insert(std::pair<std::string, sf::Shader&>(name, shader));
}

sf::Shader a_shader;

add_Shader(a_shader, "a name");  // Now it should work
 

Now, since you probably want that your std::map holds a shader, the upper solution will also not work, because the shader that is already created needs to live long enough for reference to work, and you want your maps to have actual shaders, not references.

Maybe the best course of action is the use of smart pointers:
void add_Shader(std::string name)   // Note that there is no shader here
{
    // Create a shader here using smart pointers
    std::unique_ptr<sf::Shader>  a_Shader(new sf::Shader);
    // Set everything you need for that shader here.

    //  You will also need to make a std::map hold "std::unique_ptr<sf::Shader>" instead of "sf::Shader",
    // then insert it using the "std::move()" function:
    a_map.insert(std::pair<std::string, std::unique_ptr<sf::Shader>>(name, std::move(a_Shader)));
}
add_Shader("a name");  // It should work too.
 

Of course there are other solutions as well, but this should be enough. :)
You could also pass a smart pointer as a function argument, that should work too.

Edit: minor code correction
« Last Edit: January 08, 2016, 01:52:10 pm by Brax »

9100eric

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Shadermanager [Problem with Noncopyable]
« Reply #3 on: January 08, 2016, 11:06:35 am »
Okay, thank you  :)

 

anything