小编TNT*_*aks的帖子

编译时出现矢量push_back错误

编译这段涉及对向量使用push_back函数的代码最终会出错.

for (int i=0; i<=n; i++)
{
    if(i==0)
    {
        Profit[i].push_back(0);
        Weight[i].push_back(0);
        Name[i].push_back("");
    }
    else
    {
        Profit[i].push_back(tosteal[i-1].getProfit());
        Weight[i].push_back(tosteal[i-1].getWeight());
        Name[i].push_back(tosteal[i-1].getName());
    }   
}
Run Code Online (Sandbox Code Playgroud)

Weight和Profit是int数据类型的声明向量,Name是字符串数据类型的向量.tosteal是一个项目对象的数组.getProfit()和getWeight()返回一个int,getName()返回一个字符串.

这些是编译器给出的错误,有些是重复:

joulethiefdynamicrefined.cpp:167:错误:请求'Profit.std :: vector <_Tp,_Alloc> :: operator []中的成员'push_back'[with _Tp = int,_Alloc = std :: allocator]((long long unsigned int)i))',它是非类型'int'joulethiefdynamicrefined.cpp:168:错误:请求'Weight.std :: vector <_Tp,_Alloc> :: operator []中的成员'push_back' with _Tp = int,_Alloc = std :: allocator]((long unsigned int)i))',这是非类型'int'joulethiefdynamicrefined.cpp:169:错误:从'const char*'转换无效'char'joulethiefdynamicrefined.cpp:169:错误:初始化'void std :: basic_string <_CharT,_Traits,_Alloc> :: push_back(_CharT)的参数1 [_CharT = char,_ Traits = std :: char_traits,_Alloc = std :: allocator]'joulethiefdynamicrefined.cpp:173:错误:请求'Profit.std :: vector <_Tp,_Alloc> :: operator []中的成员'push_back'[with …

c++ vector push-back

-2
推荐指数
1
解决办法
966
查看次数

标签 统计

c++ ×1

push-back ×1

vector ×1