저는 나뭇 가지로 구성된 나무를 만들고 있습니다. 내 일을 위해서, 나는 가지를 추적해야하고, 그렇게하기 위해서, 나는 그것들을 벡터 목록에 저장하고 싶다. 아래 코드 스 니펫에 표시된 생성자와 함수 모두에서이 벡터 변수를 전역 변수로이 파일에 저장합니다.벡터 목록을 전역 변수로 저장할 수 있습니까?
여기 까다로운 부분은 내가 알 수있는 한 반복기가 제대로 작동하지 않는 것과 관련이 있다는 오류 메시지 (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