循环访问包含自定义类C++的向量
Iterating through a vector which contains a custom class C++
我希望有人能帮助我。为了更具体地说明我真正需要什么,并精简我的代码,我已经从纯粹的类向量更改为具有新类的对象向量,我的原始类是其中的类型。
我希望到目前为止我已经清楚地解释了自己。我将展示相关的类:
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"中使用了一个成员函数,从特定对象返回我想要的任何值:
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;
}
}
}
然而。。。它返回垃圾。我显然错过了一些东西,但我正在逻辑上经历它,这一切似乎都是有道理的。
如果事情很简单,提前抱歉!我很欣赏这可能看起来很冗长,但我真的很感激一些帮助!
而且要超级清楚...这就是我所说的:
vector<bet> localbuttons; //Declaration of Vector
load_map("data.dat", &localbuttons); //load buttonmap using function
int buttonpressed = returnbuttonid(100,300, &localbuttons);
为了回应一个非常快速的评论。很明显,问题至少始于一段未发布的代码。当我尝试重载构造函数时,我的"bet"向量没有被我传递给它的参数填充。我假设我在创建新类"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",但是如果我更改它,它会相应地更改。
我不应该说垃圾,我有错,因为我认为我已经正确地确定了问题的领域,并试图简洁。所以我想我应该先问...如何正确重载此"屏幕区域"构造函数?
这里的问题出在 Bet 类的构造函数中。
看完这里:http://www.cplusplus.com/doc/tutorial/classes/
我在 Bet 类中重写了构造函数:
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)
{};
如果我浪费了任何人的时间误导,我深表歉意,并感谢乔纳森波特的明智建议。
我不确定为什么我认为您可以在括号内调用构造函数。我的编译器似乎没有抱怨它,但从我所能收集到的信息来看 - 我只是在创建一个临时对象。
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么