静态向量push_back崩溃 [英] static vector push_back crashes

查看:225
本文介绍了静态向量push_back崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在向A类中的静态向量添加项目时遇到了崩溃;



static std :: vector< pegshort>键;



在另一个类中,我尝试按如下方式插入项目;



A :: keys.push_back(x); //程序在此代码块中崩溃

(x是一个pegshort类型变量)



当我为非执行所有步骤时静态矢量它工作正常。但是我需要一个静态向量。

你能帮忙解决一下崩溃的原因吗?

Hi,
I got a crash while adding item to a static vector in class A ;

static std::vector< pegshort > keys;

In another class, I try to insert item as follows ;

A::keys.push_back(x); // the program crashes in this code block
(x is a pegshort type variable )

When I do all steps for a non-static vector it works OK. But I need a static vector.
Could you please help about the cause of crash ?

推荐答案

// On-Demand Test.cpp :)

#include "pegshort.h"
#include <vector>

typedef std::vector<pegshort> vecKeys;

vecKeys& GetStaticKeys()
{
  static vecKeys s_keys;
  return s_kes;
}

class Test
{
public:
  Test()
  {
    GetStaticKeys().push_back(pegshort(3));
  }
} testIt;


这篇关于静态向量push_back崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆