【问题标题】:Iterating through a vector which contains a custom class C++遍历包含自定义类 C++ 的向量
【发布时间】:2016-07-16 20:16:06
【问题描述】:

我希望有人可以帮助我。为了更具体地了解我真正需要的内容,并精简我的代码,我已经从拥有一个纯粹属于我的类的向量变为拥有一个新类的对象向量,我的原始类是其中的一种类型之内。

我希望到目前为止我已经清楚地解释了自己。我将展示相关的类:

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;
        }
    }

我已经修改了一个查找函数,以便在屏幕区域上使用这个成员函数,它是“赌注”中包含的一种类型。

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”时已正确更正了语法,但在探测向量后它没有显示任何数据。

在我的函数 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”构造函数?

【问题讨论】:

  • “它返回垃圾”没有为任何人提供足够的问题信息来真正帮助您。您显示的代码存在一些问题,但如果没有minimal reproducible example,几乎不可能知道问题(无论是什么)可能出在哪里。你在检查load_map() 是否真的有效吗?向量是否包含您认为的数据?您是否已在调试器中单步执行以查看“垃圾”来自何处?
  • 好的,感谢您的反馈。我将更新问题以回应您指出的内容!

标签: c++ function vector overloading member


【解决方案1】:

这里的问题出在 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)
{};

如果我因误导而浪费了任何人的时间,我深表歉意,并感谢 Jonathon Potter 的明智建议。

我不知道为什么我认为你可以在括号内调用构造函数。我的编译器似乎没有抱怨它,但据我所知 - 我只是在创建一个临时对象。

【讨论】:

    猜你喜欢
    • 2012-11-09
    • 1970-01-01
    • 2015-04-21
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    相关资源
    最近更新 更多