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:
#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:
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:
#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:
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!