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

Author Topic: [SOLVED]vector of class owning a render window  (Read 3308 times)

0 Members and 1 Guest are viewing this topic.

short_name

  • Newbie
  • *
  • Posts: 14
    • View Profile
[SOLVED]vector of class owning a render window
« on: December 22, 2011, 07:01:14 am »
I am trying to create a vector of a class which owns a render window and I think I am running into a problem because the render window is non copyable. I am able to pass a reference of the render window to the constructor but when I try to push the object I create onto the vector I run into trouble.

My code looks like this:
Code: [Select]

#include <SFML/Graphics.hpp>
#include <vector>

using namespace std;
class GameObject {
  public:
    GameObject::GameObject( sf::RenderWindow &render_window ) : render_window( render_window )
    {
    };

  private:
    sf::RenderWindow& render_window;
};

int main()
{
  vector<GameObject> test;
  sf::RenderWindow app;
  GameObject go (app);
  test.push_back(go);
}

but when I try to compile I get the output:
Code: [Select]

g++ -o game example.cpp -lsfml-graphics -lsfml-window -lsfml-system
example.cpp:8:5: error: extra qualification âGameObject::â on member âGameObjectâ [-fpermissive]
example.cpp: In member function âGameObject& GameObject::operator=(const GameObject&)â:
example.cpp:6:7:   instantiated from âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = GameObject, _Alloc = std::allocator<GameObject>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<GameObject*, std::vector<GameObject> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = GameObject*]â
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:834:4:   instantiated from âvoid std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = GameObject, _Alloc = std::allocator<GameObject>, std::vector<_Tp, _Alloc>::value_type = GameObject]â
example.cpp:22:20:   instantiated from here
example.cpp:6:7: error: non-static reference member âsf::RenderWindow& GameObject::render_windowâ, canât use default assignment operator
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/vector:70:0,
                 from /usr/include/SFML/Window/VideoMode.hpp:32,
                 from /usr/include/SFML/Window.hpp:39,
                 from /usr/include/SFML/Graphics.hpp:32,
                 from example.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc: In member function âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = GameObject, _Alloc = std::allocator<GameObject>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<GameObject*, std::vector<GameObject> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = GameObject*]â:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc:317:4: note: synthesized method âGameObject& GameObject::operator=(const GameObject&)â first required here


I tried adding a copy constructor:
Code: [Select]

#include <SFML/Graphics.hpp>
#include <vector>

using namespace std;
class GameObject {
  public:
    GameObject::GameObject( sf::RenderWindow &render_window ) : render_window( render_window )
    {
    };
    GameObject( const GameObject& game_object ) : render_window( game_object.render_window )
    {
    };

  private:
    sf::RenderWindow& render_window;
};

int main()
{
  vector<GameObject> test;
  sf::RenderWindow app;
  GameObject go (app);
  test.push_back(go);
}


but then I get the very similar output:
Code: [Select]

g++ -o game example2.cpp -lsfml-graphics -lsfml-window -lsfml-system
example2.cpp:7:5: error: extra qualification âGameObject::â on member âGameObjectâ [-fpermissive]
example2.cpp: In member function âGameObject& GameObject::operator=(const GameObject&)â:
example2.cpp:5:7:   instantiated from âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = GameObject, _Alloc = std::allocator<GameObject>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<GameObject*, std::vector<GameObject> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = GameObject*]â
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_vector.h:834:4:   instantiated from âvoid std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = GameObject, _Alloc = std::allocator<GameObject>, std::vector<_Tp, _Alloc>::value_type = GameObject]â
example2.cpp:23:20:   instantiated from here
example2.cpp:5:7: error: non-static reference member âsf::RenderWindow& GameObject::render_windowâ, canât use default assignment operator
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/vector:70:0,
                 from /usr/include/SFML/Window/VideoMode.hpp:32,
                 from /usr/include/SFML/Window.hpp:39,
                 from /usr/include/SFML/Graphics.hpp:32,
                 from example2.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc: In member function âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = GameObject, _Alloc = std::allocator<GameObject>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<GameObject*, std::vector<GameObject> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = GameObject*]â:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/vector.tcc:317:4: note: synthesized method âGameObject& GameObject::operator=(const GameObject&)â first required here


I am sure I am missing something obvious as I am new to this but any responses will be greatly appreciated!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED]vector of class owning a render window
« Reply #1 on: December 22, 2011, 08:33:06 am »
You need an assignment operator (operator=) in addition to the copy constructor.
Laurent Gomila - SFML developer

short_name

  • Newbie
  • *
  • Posts: 14
    • View Profile
danke schön
« Reply #2 on: December 22, 2011, 09:08:31 am »
Thanks Laurent! I have never written an assignment operator before but I figured it out with a little help from scroogle  :D .

Here is the new compilable version of code for anyone who is interested:
Code: [Select]

#include <SFML/Graphics.hpp>
#include <vector>

using namespace std;
class GameObject {
  public:
    GameObject( sf::RenderWindow &render_window ) : render_window( render_window )
    {
    };
    GameObject( const GameObject& game_object ) : render_window( game_object.render_window )
    {
    };
    GameObject& operator= (const GameObject & game_object )
    {
    };

  private:
    sf::RenderWindow& render_window;
};

int main()
{
  vector<GameObject> test;
  sf::RenderWindow app;
  GameObject go (app);
  test.push_back(go);
}
[\code]

 

anything