C++我是否正确添加和访问了我的Vector
C++ Am I adding and accessing my Vector correctly?
我正在创建一个对象并将其添加到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
时,编译器将选择第二个常量重载。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的好友类无法访问私人会员?
- 为什么我的 setter 方法会产生错误的访问错误
- 如何为我的容器实现随机访问迭代器?
- 访问我以前可以访问的功能时出现分段错误
- 为什么访问我的引用捕获变量会导致我的 lambda 函数出现段错误?
- 在不同条件下无法访问我的向量变量
- 如何在OMNet++中访问我的模拟的不同节点
- 如何使用Visual Studio CppunittestFramework访问我的代码
- 如何使用QT/C 中的多线程用API访问我的数据库
- CMake没有创建可以访问我的共享库的可执行文件
- 我无法使用C++访问我的类的属性
- 如何访问我的子类?C++
- C++在另一个头文件和cpp中访问我的矢量
- 为什么我无法访问我的成员变量
- 为什么我无法在全局范围内访问我的数组下标
- 无法访问我的网络摄像头opencv ubuntu
- 为什么我不能在c++中访问我的私有变量和getter和setter消息
- 如何在使用QQmlApplicationEngine时从C++访问我的Window对象属性
- Eclipse - 无法访问我的文件夹中的 cpp 文件