C++我是否正确添加和访问了我的Vector

C++ Am I adding and accessing my Vector correctly?

本文关键字:访问 我的 Vector 添加 是否 C++      更新时间:2023-10-16

我正在创建一个对象并将其添加到std::vector中,然后访问std::vector以使用我放置在其中的对象中的成员变量。

.h,其中包含std::vector

std::vector<Field *> vFields;
inline std::vector<Field *> getFields() { return vFields; };

在.cpp中,我这样做:

Field* f1 = Field::createWithLocation(ccp( p.x, p.y));
addChild(f1->getFieldSprite(), 2);
getFields().push_back(f1); // add the new field to a container
//vFields.push_back(f1); // add the new field to a container
std::cout << "add - # of Fields: " << getFields().size() << std::endl;

当实例化上面的Field* f1时,会发生以下情况:

Field* Field::createWithLocation(cocos2d::CCPoint p)
{
    Field* f = new Field();
    //f->autorelease();
    f->initWithLocation(p);
    return f;
}
void Field::initWithLocation(cocos2d::CCPoint p)
{
    setFieldCenterPoint(p);
    setFieldGraphicName(FIELD::fieldIconFileName);
    setFieldSprite(cocos2d::CCSprite::create(getFieldGraphicName().c_str()));
     getFieldSprite()->setPosition(ccp(getFieldCenterPoint().x, getFieldCenterPoint().y));
    setFieldSize(getFieldSprite()->getContentSize());
    setFieldNumber(7159);
    setFieldTag(7159);
    std::cout << "When Field Created #: " << getFieldTag() << std::endl;
}

这很好,当创建Field对象时,std::vector会在size()中显示1,getFieldTag()会像设置的那样返回7159

问题是,当我从向量访问Field时,发生了一些事情,我崩溃了。我发现如果我输出getTagNumber(),它会大不相同。

访问矢量的示例:

else if (getFieldLayerState() == kActive)
{
    // so did they click on a field?
    std::cout << "Field Layer Status Is Active" << std::endl;
    std::cout << "touch - # of Fields: " << vFields.size() << std::endl;
    // Loop through the field vector and get the size of each field and see if it was tapped    
    for (int f=0; f<vFields.size(); f++)
    {
        //field = (Field*) getFields().at(f);
        Field* field = (Field*) vFields.at(f);
        std::cout << "field #: "<< field->getFieldNumber() << std::endl;
        std::cout << "field tag: "<< field->getFieldTag() << std::endl;
        if (field->getFieldSprite()->boundingBox().containsPoint(location))
        {
            std::cout << "touched field #: "<< field->getFieldNumber() << std::endl;
            std::cout << "touched field tag: "<< field->getFieldTag() << std::endl;
            _dir->Instance()->getAudioEngine()->playBackgroundMusic("tap.mp3", false);    
        }
     }
}

cout语句的示例输出:

When Field Created #: 7159
add - # of Fields: 1
Field Layer Status Is Active
touch - # of Fields: 1
field #: 1769234804
field tag: 353394533`

我用EXEC_BAD_ACCESS在上面的if (field->getFieldSprite()->boundingBox().containsPoint(location))线上崩溃,很明显,向量中的内容与我放入的内容发生了变化,或者看起来是这样。

有人能帮我理解我做错了什么吗?

getFields函数返回向量的副本。因此,对返回向量的任何更改都只会发生在副本中,而不会发生在原始向量中。

您希望返回引用

std::vector<Field*>& getFields() { return vFields; }
//                 ^
// Note the ampersand

您可能还想添加函数的const重载:

const std::vector<Field*>& getFields() const { return vFields; }
// ^                                      ^
// Note the two `const` modifiers, the last one is important

然后,如果修改向量,例如添加到向量中,编译器将选择第一个非常量重载。如果您不修改向量,例如,当您只对向量调用size时,编译器将选择第二个常量重载。