简体   繁体   中英

Problem properly pushing 2D vector of structs (C++)

I am having trouble filling a vector of type vector<vector<clickable> > . The compiler seems to be ok with pushing back vector<clickable> 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.

In the following examples, the loops should be exactly the same, except that one calls push_back on a vector<vector<clickable> > that was just declared in the same function, and the other calls push_back on one declared in the .h file.

Example of what works (This is from the main function, but it works in any function.):

vector<vector<clickable> > clicks;
for(int i = 0; i < 10; i++){
    vector<clickable> click;
    for(int j = 0; j < 10; j++){
            click.push_back(clickable(Rect(Point(50,50),5,10),"blar"));
    }
    clicks.push_back(click);
}

Example of what doesn't work:

Gui.h:

#include <vector>
//...
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<vector<clickable> > ship;
    //...
}

Gui.cpp:

#include "Gui.h"
//...
void Gui::load_environment(){
    for(int i = 0; i < 10; i++){
            vector<clickable> click;
            for(int j = 0; j < 10; j++){
                    click.push_back(clickable(Rect(Point(50,50),5,10),
                                            "blar"));
            }
            ship.push_back(click);
    }
}
//...

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.

edit Here is the text of the error:

Gui.cpp:47: error: no matching function for call to 'std::vector<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >::push_back(std::vector<clickable, std::allocator<clickable> >&)' /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<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Alloc = std::allocator<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >]

The error message says, in effect, that it thinks the vector you're trying to push_back onto is actually a vector<vector<string> > , rather than a vector<vector<clickable> > as you were expecting.

You probably just need to do a clean rebuild.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM