遍历包含自定义 class C++ 的向量
Iterating through a vector which contains a custom class C++
我希望有人能帮助我。为了更具体地说明我真正需要什么,并 trim 写下我的代码,我已经从拥有一个纯粹由我的 Class 组成的向量,转变为拥有一个新 [=] 对象的向量52=],其中我原来的class是里面的一个类型。
我希望到目前为止我已经解释清楚了。我会展示相关的 classes:
class screen_area
{
private:
int my_id, my_x, my_y, my_width, my_height;
bool active=true;
public:
screen_area (int button_id=0, int x=0, int y=0, int width=0, int height=0, bool isactive=true)
{
my_id = button_id;
my_x = x;
my_y = y;
my_width = width;
my_height = height;
active = isactive;
}
~screen_area()
{}
class bet
{
private:
int wager = 0;
int multiplier = 0;
public:
screen_area area;
bet(int wager, int multiplier, screen_area area)
{};
~bet()
{};
他们还有更多,但这是面包和黄油。现在以前我在 "screenarea" 中使用了一个成员函数,到 return 我想要从特定对象获得的任何值:
int getvalue(int value)
{
switch(value)
{
case 1 :
return my_id;
case 2 :
return my_x;
case 3 :
return my_y;
case 4 :
return my_width;
case 5 :
return my_height;
case 6 :
return active;
}
}
并且我修改了一个查找函数以在 "bet".
中包含的类型的屏幕区域上使用此成员函数
int returnbuttonid(int mousex, int mousey, std::vector<bet> *buttons)
{
for (auto ep : *buttons )
{
if ((ep.area.getvalue(2) > mousex) && (ep.area.getvalue(3) > mousey))
{int id_value = ep.area.getvalue(1);
return id_value;
}
}
}
但是...它 return 是垃圾。我显然遗漏了一些东西,但我正在逻辑上进行它,这一切似乎都是有道理的。
如果事情很简单,请提前道歉!我很欣赏这可能看起来很啰嗦,但我真的很感激一些帮助!
而且非常清楚......我就是这样称呼它的:
vector<bet> localbuttons; //Declaration of Vector
load_map("data.dat", &localbuttons); //load buttonmap using function
int buttonpressed = returnbuttonid(100,300, &localbuttons);
回应非常快速的评论。很明显,问题至少始于一段未发布的代码。当我尝试重载构造函数时,我的 "bet" 向量没有被传递给它的参数填充。我假设我在创建新的 class "bet" 时正确地更正了语法,但是在探测矢量之后它没有显示任何数据。
在我的函数中 load_map:
bool load_map(std::string path, std::vector<bet> *buttons)
{
//setup file
ifstream inputFile( path.c_str() );
//
//The stuff in the middle here is irrelevant
//and I've take it out to make this tidier
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
}
return 0;
}
自从我使用此功能以来,唯一发生变化的部分是:
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
所以我猜这就是问题的根源。变量没有重载默认的 screen_area 构造函数。所以当我:
cout << localbuttons[1].area.my_id << endl;
我总是看到我在默认构造函数中放置的任何值。它在我在这里发布的构造函数中是“0”,但如果我改变它,它也会相应地改变。
而且我不应该说乱七八糟的,我错了,因为我认为我已经正确地确定了问题的范围,并试图简明扼要。所以我想我应该先问...我怎样才能正确重载这个 "screenarea" 构造函数?
这里的问题出在赌注的构造函数中 class。
看了这里之后:
http://www.cplusplus.com/doc/tutorial/classes/
我重写了Bet中的构造函数class:
bet(int w, int m, int button_id=0, int x=0, int y=0,
int width=0, int height=0, bool isactive=true)
: area(button_id, x, y, width, height, isactive),
wager(w), multiplier(m)
{};
如果我误导了任何人,我深表歉意,感谢 Jonathon Potter 的明智建议。
我不确定为什么我认为您可以在括号内调用构造函数。我的编译器似乎没有抱怨它,但据我所知——我只是在创建一个临时对象。
我希望有人能帮助我。为了更具体地说明我真正需要什么,并 trim 写下我的代码,我已经从拥有一个纯粹由我的 Class 组成的向量,转变为拥有一个新 [=] 对象的向量52=],其中我原来的class是里面的一个类型。
我希望到目前为止我已经解释清楚了。我会展示相关的 classes:
class screen_area
{
private:
int my_id, my_x, my_y, my_width, my_height;
bool active=true;
public:
screen_area (int button_id=0, int x=0, int y=0, int width=0, int height=0, bool isactive=true)
{
my_id = button_id;
my_x = x;
my_y = y;
my_width = width;
my_height = height;
active = isactive;
}
~screen_area()
{}
class bet
{
private:
int wager = 0;
int multiplier = 0;
public:
screen_area area;
bet(int wager, int multiplier, screen_area area)
{};
~bet()
{};
他们还有更多,但这是面包和黄油。现在以前我在 "screenarea" 中使用了一个成员函数,到 return 我想要从特定对象获得的任何值:
int getvalue(int value)
{
switch(value)
{
case 1 :
return my_id;
case 2 :
return my_x;
case 3 :
return my_y;
case 4 :
return my_width;
case 5 :
return my_height;
case 6 :
return active;
}
}
并且我修改了一个查找函数以在 "bet".
中包含的类型的屏幕区域上使用此成员函数int returnbuttonid(int mousex, int mousey, std::vector<bet> *buttons)
{
for (auto ep : *buttons )
{
if ((ep.area.getvalue(2) > mousex) && (ep.area.getvalue(3) > mousey))
{int id_value = ep.area.getvalue(1);
return id_value;
}
}
}
但是...它 return 是垃圾。我显然遗漏了一些东西,但我正在逻辑上进行它,这一切似乎都是有道理的。
如果事情很简单,请提前道歉!我很欣赏这可能看起来很啰嗦,但我真的很感激一些帮助!
而且非常清楚......我就是这样称呼它的:
vector<bet> localbuttons; //Declaration of Vector
load_map("data.dat", &localbuttons); //load buttonmap using function
int buttonpressed = returnbuttonid(100,300, &localbuttons);
回应非常快速的评论。很明显,问题至少始于一段未发布的代码。当我尝试重载构造函数时,我的 "bet" 向量没有被传递给它的参数填充。我假设我在创建新的 class "bet" 时正确地更正了语法,但是在探测矢量之后它没有显示任何数据。
在我的函数中 load_map:
bool load_map(std::string path, std::vector<bet> *buttons)
{
//setup file
ifstream inputFile( path.c_str() );
//
//The stuff in the middle here is irrelevant
//and I've take it out to make this tidier
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
}
return 0;
}
自从我使用此功能以来,唯一发生变化的部分是:
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
所以我猜这就是问题的根源。变量没有重载默认的 screen_area 构造函数。所以当我:
cout << localbuttons[1].area.my_id << endl;
我总是看到我在默认构造函数中放置的任何值。它在我在这里发布的构造函数中是“0”,但如果我改变它,它也会相应地改变。
而且我不应该说乱七八糟的,我错了,因为我认为我已经正确地确定了问题的范围,并试图简明扼要。所以我想我应该先问...我怎样才能正确重载这个 "screenarea" 构造函数?
这里的问题出在赌注的构造函数中 class。
看了这里之后: http://www.cplusplus.com/doc/tutorial/classes/
我重写了Bet中的构造函数class:
bet(int w, int m, int button_id=0, int x=0, int y=0,
int width=0, int height=0, bool isactive=true)
: area(button_id, x, y, width, height, isactive),
wager(w), multiplier(m)
{};
如果我误导了任何人,我深表歉意,感谢 Jonathon Potter 的明智建议。
我不确定为什么我认为您可以在括号内调用构造函数。我的编译器似乎没有抱怨它,但据我所知——我只是在创建一个临时对象。