vector insert()警告消息

时间:2016-12-12 21:37:08

标签: c++ vector iterator

我是一名c ++初学者。我在代码中收到此警告消息:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(208): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<double&>(int *,_Other)' being compiled
1>          with
1>          [
1>              _Ty=int,
1>              _Other=double &
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(668) : see reference to function template instantiation 'void std::_Cons_val<std::allocator<_Ty>,int,double&>(_Alloc &,_Ty1 *,_Ty2)' being compiled
1>          with
1>          [
1>              _Ty=int,
1>              _Alloc=std::allocator<int>,
1>              _Ty1=int,
1>              _Ty2=double &
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(688) : see reference to function template instantiation 'void std::vector<_Ty>::emplace_back<double&>(_Valty)' being compiled
1>          with
1>          [
1>              _Ty=int,
1>              _Valty=double &
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(675) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::emplace<double&>(std::_Vector_const_iterator<_Myvec>,_Valty)' being compiled
1>          with
1>          [
1>              _Myvec=std::_Vector_val<int,std::allocator<int>>,
1>              _Ty=int,
1>              _Valty=double &
1>          ]
1>          preprocessDoc.cpp(14054) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::insert<double&>(std::_Vector_const_iterator<_Myvec>,_Valty)' being compiled
1>          with
1>          [
1>              _Myvec=std::_Vector_val<int,std::allocator<int>>,
1>              _Ty=int,
1>              _Valty=double &
1>          ]

代码编译没有任何错误但是我想摆脱这个警告。我收到警告的行是:

void PreprocessDoc::AddDisplay(double d_display, int pattern)
{
    if(pattern==1)
    {
        d_displayYT.insert(d_displayYT.end(), d_display);  //this line
        YTtoYrn();
    }
}

任何帮助都会很好

2 个答案:

答案 0 :(得分:2)

_Myvec=std::_Vector_val<int,std::allocator<int>>,

编译器告诉您正在将double转换为int。如果双重说42.666,则存储的内容仅为42。如果不是这样的话,它会警告你。

如果这真的是你想要的,那就做David suggests。如果不是那么你需要改变矢量的类型。

答案 1 :(得分:1)

变化:

    d_displayYT.insert(d_displayYT.end(), d_display);  //this line

为:

    d_displayYT.insert(d_displayYT.end(), static_cast<int>(d_display));