【发布时间】:2017-08-01 16:25:47
【问题描述】:
class TrackSymbol
{
protected: static std::map<int, std::vector<char> > _trackTypeToIdentificationCodeMap ;
protected: static char _identificationCodeChars[][2] ;
} ;
在实现文件中:
std::map<int, std::vector<char> > TrackSymbol::_trackTypeToIdentificationCodeMap ;
char TrackSymbol::_identificationCodeChars[][2] =
{
{ ' ', ' ' },
{ 'S', '6' },
{ 'Z', 'U' }
} ;
在构造函数中,我试图用 _identificationCodeChars 中的适当值填充本地向量,但在副本中遇到了段错误:
for ( int i = 0 ; i < sizeof( _identificationCodeChars ) / sizeof( _identificationCodeChars[0] ) ; i++ )
{
std::vector<char> vec ;
vec.push_back( _identificationCodeChars[i][0] ) ;
vec.push_back( _identificationCodeChars[i][1] ) ;
_trackTypeToIdentificationCodeMap[i] = vec ; //Segfault here
}
我的理解是赋值运算符会对本地向量进行硬拷贝,所以我对它为什么会遇到分段错误感到有些困惑。另外,在我当前的环境中,我仅限于 C98,所以我无法使用扩展初始化列表等内容。
【问题讨论】:
-
请编辑您的问题以包含minimal reproducible example
标签: c++ vector stl segmentation-fault