c++ - 如何将 vector 列表存储为全局变量?

标签 c++ class struct stdvector

我正在创建一棵由 Twig 组成的树。出于我的工作目的,我需要跟踪分支,为此,我想将它们存储在 vector 列表中。我将 vector 列表作为全局变量存储在此文件中,因为我想在下面代码片段中显示的构造函数和函数中使用它。

这里棘手的部分是我收到一条错误消息(在 Visual Studio 2013 中运行),据我所知,这与迭代器未正确完成其工作有关。每当我调用 branchList.push_back(root) 和 branchList.resize() 时都会出现错误消息。 branchList.size() 不会导致错误。

所以我的问题是:我缺少/不理解什么才能使这项工作成功?如果我要放置 vector branchList;在构造函数的开头,一切都按预期进行。然而,这对我没有帮助,因为我以后还需要在其他功能中使用它。

我正在使用的文件中的相关代码片段。

skeletonBuilder.h:

class TreeSkeleton {

public:
    TreeSkeleton();
    void growTree();
};

骨架生成器.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
}

主要.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.

错误消息将我带到名为“vector”的文件中的以下代码片段:

 #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();
                }
    }

最佳答案

在实现文件之间不保证全局对象的初始化顺序。没有办法知道 main.cpp 的全局变量或 skeletonBuilder.cpp将首先被初始化。在你的情况下,TreeSkeleton treestd::vector<branch> branchList 之前初始化这会导致你的问题。 TreeSkeleton 的构造函数必须使用未初始化的 branchList这是未定义的行为。解决方案是以保证顺序的方式放置全局变量。

一个解决方案是制作branchList局部静态变量。这些变量保证在第一次遇到时被初始化。

例如:

class TreeSkeleton {

public:
    TreeSkeleton();
    void growTree();

private:
    static std::vector<branch> & getBranches();
};

std::vector<branch> & TreeSkeleton::getBranches()
{
    // branchList is initialized the first time this line is encountered
    static std::vector<branch> branchList;
    return 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);

    auto & branchList = getBranches();
    branchList.size();
    branchList.push_back(root); // Should be fine now
}

关于c++ - 如何将 vector 列表存储为全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46606306/

相关文章:

c++ - Visual Studio 2013 msvcr120 到 msvcr100

Java(安卓): calling a function from Context without cast

没有括号的 Javascript 调用类(jQuery 风格)

类中的python生成器

c++ - 异常规范在声明和功能实现上不兼容

c++ - 在现代 OpenGL 和 C++ 中渲染 Quake BSP

ios - 将结构与 UIImage 进行比较

c# - 从结构继承

c++ - 超出范围的局部变量......我认为

c++ - 延迟渲染 : Performance issues