[英]Problem properly pushing 2D vector of structs (C++)
I am having trouble filling a vector of type vector<vector<clickable> >
. 我在填充
vector<vector<clickable> >
类型的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. 只要push_back发生在与变量声明相同的函数中,编译器似乎可以将
vector<clickable>
推回到它上面,但是当变量在.h文件中声明时它不允许它push_back在类的另一个函数中执行。
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. 在下面的示例中,循环应该完全相同,除了在同一函数中声明的
vector<vector<clickable> >
上调用push_back,而另一个调用.h文件中声明的push_back。
Example of what works (This is from the main function, but it works in any function.): 什么有用的例子(这是来自main函数,但它适用于任何函数。):
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: 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: 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. 该错误消息指出,实际上,它认为你想在矢量
push_back
到实际上是一个vector<vector<string> >
而不是一个vector<vector<clickable> >
如您所期望。
You probably just need to do a clean rebuild. 您可能只需要进行干净的重建即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.