我正在创建一个由分支组成的树。为了我的工作目的,我需要跟踪分支,为了做到这一点,我想将它们存储在向量列表中。我将vector-list作为全局变量存储在此文件中,因为我想在构造函数和下面的代码片段中显示的函数中使用它。如何将矢量列表存储为全局变量?
这里比较棘手的部分是,我收到一条错误消息(在Visual Studio 2013中运行),据我所知可能与迭代器没有正常工作有关。每当我调用branchList.push_back(root)和branchList.resize()时都会出现错误消息。 branchList.size()不会导致错误。
所以我的问题是:我错过了什么/不理解,使这项工作?如果我要放置向量branchList;在构造函数的开始,一切都按预期工作。但是这对我没有帮助,因为我以后需要在其他功能中使用它。
我正在使用的文件中的相关代码片段。
skeletonBuilder.h:
class TreeSkeleton {
public:
TreeSkeleton();
void growTree();
};
skeletonBuilder.cpp:
#include "skeletonBuilder.h"
#include <cstdint>
#include <vector>
typedef struct branch {
branch *parent;
vec3 position;
vec3 direction;
} branch;
//used by constructor + "treeGrow" function
std::vector<branch> branchList = {};
TreeSkeleton::TreeSkeleton() {
//instantiate the tree root as a starting position.
branch root;
root.parent = NULL;
root.position = vec3(0, 0, 0);
root.direction = vec3(0, 1, 0);
branchList.size(); //works fine
branchList.resize(100); //Crashes here
branchList.push_back(root); //Crashes here
}
TreeSkeleton::growTree() {
//pushing more branches to branchList
}
main.cpp中:
#include "skeletonBuilder.h"
TreeSkeleton tree;
int main(int argc, char *argv[]) {
return 0;
}
该错误消息我得到:
Unhandled exception at 0x00507077 in OpenGL_project_Debug.exe: 0xC0000005: Access violation reading location 0x40EAAAB4.
错误消息带我到下面的代码片断在一个名为“矢量”文件:
#if _VECTOR_ORPHAN_RANGE
void _Orphan_range(pointer _First, pointer _Last) const
{ // orphan iterators within specified (inclusive) range
_Lockit _Lock(_LOCK_DEBUG);
const_iterator **_Pnext = (const_iterator **)this->_Getpfirst();
if (_Pnext != 0)
while (*_Pnext != 0) //<----------------This is the row that it gets stuck on
if ((*_Pnext)->_Ptr < _First || _Last < (*_Pnext)->_Ptr)
_Pnext = (const_iterator **)(*_Pnext)->_Getpnext();
else
{ // orphan the iterator
(*_Pnext)->_Clrcont();
*_Pnext = *(const_iterator **)(*_Pnext)->_Getpnext();
}
}
请复制粘贴你得到 – UnholySheep