Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem properly pushing 2D vector of structs (C++)
    primarykey
    data
    text
    <p>I am having trouble filling a vector of type <code>vector&lt;vector&lt;clickable&gt; &gt;</code>. The compiler seems to be ok with pushing back <code>vector&lt;clickable&gt;</code> onto it as long as the push_back happens in the same function as the declaration of the variable, but it doesn't allow it when the variable is declared in a .h file and the push_back is executed in another function of the class.</p> <p>In the following examples, the loops should be exactly the same, except that one calls push_back on a <code>vector&lt;vector&lt;clickable&gt; &gt;</code> that was just declared in the same function, and the other calls push_back on one declared in the .h file.</p> <p>Example of what works (This is from the main function, but it works in any function.):</p> <pre><code>vector&lt;vector&lt;clickable&gt; &gt; clicks; for(int i = 0; i &lt; 10; i++){ vector&lt;clickable&gt; click; for(int j = 0; j &lt; 10; j++){ click.push_back(clickable(Rect(Point(50,50),5,10),"blar")); } clicks.push_back(click); } </code></pre> <p>Example of what doesn't work:</p> <p>Gui.h:</p> <pre><code>#include &lt;vector&gt; //... struct clickable { Rect rect; string msg; bool visible; clickable(Rect rectangle, string message){ rect = rectangle; msg = message; visible = true; } }; //... class Gui{ public: //... void load_environment(); //... private: vector&lt;vector&lt;clickable&gt; &gt; ship; //... } </code></pre> <p>Gui.cpp:</p> <pre><code>#include "Gui.h" //... void Gui::load_environment(){ for(int i = 0; i &lt; 10; i++){ vector&lt;clickable&gt; click; for(int j = 0; j &lt; 10; j++){ click.push_back(clickable(Rect(Point(50,50),5,10), "blar")); } ship.push_back(click); } } //... </code></pre> <p>I think it might have something to do with my failure to overload some operators, but I don't actually have any idea if that is even the cause of the problem, or what I should overload if it is.</p> <p><strong>edit</strong> Here is the text of the error:</p> <p><code>Gui.cpp:47: error: no matching function for call to ‘std::vector&lt;std::vector&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;, std::allocator&lt;std::vector&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt; &gt;::push_back(std::vector&lt;clickable, std::allocator&lt;clickable&gt; &gt;&amp;)’ /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector&lt;_Tp, _Alloc&gt;::push_back(const _Tp&amp;) [with _Tp = std::vector&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;, _Alloc = std::allocator&lt;std::vector&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;] </code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload