简体   繁体   English

编译时向量push_back错误

[英]Vector push_back error when compiling

Compiling this segment of code involving using the push_back function for vectors ends up in an error.编译这部分涉及对向量使用 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());
    }   
}

Weight and Profit are declared vectors of the int data type and Name is a vector of the string data type. Weight 和 Profit 是 int 数据类型的声明向量,Name 是 string 数据类型的向量。 tosteal is an array of items objects. tosteal 是一组 items 对象。 getProfit() and getWeight() return an int and getName() returns a string. getProfit() 和 getWeight() 返回一个 int 而 getName() 返回一个字符串。

These are the errors the compiler gives, some are repeats:这些是编译器给出的错误,有些是重复的:

167: error: request for member ‘push_back’ in ‘Profit.std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator<int>](((long unsigned int)i))’, which is of non-class type ‘int’

168: error: request for member ‘push_back’ in ‘Weight.std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator<int>](((long unsigned int)i))’, which is of non-class type ‘int’

169: error: invalid conversion from ‘const char*’ to ‘char’

169: error:   initializing argument 1 of ‘void std::basic_string<_CharT, _Traits, _Alloc>::push_back(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’

173: error: request for member ‘push_back’ in ‘Profit.std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator<int>](((long unsigned int)i))’, which is of non-class type ‘int’

174: error: request for member ‘push_back’ in ‘Weight.std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator<int>](((long unsigned int)i))’, which is of non-class type ‘int’

175: error: no matching function for call to ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >::push_back(std::string)’
 note: candidates are: void std::basic_string<_CharT, _Traits, _Alloc>::push_back(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
Profit[i].push_back(0);

Should be应该是

Profit.push_back(0);

And so on.等等。 Profit is the vector itself; Profit是向量本身; by saying Profit[i].push_back(0) , you are trying to push something into one of the elements that is already in the vector, rather than pushing something into the vector.通过说Profit[i].push_back(0) ,您试图将某些内容推入向量中已有的元素之一,而不是将某些内容推入向量中。

Since the element type is int , Profit[i] is of type int , which is why you get the error: request for member 'push_back' in [...] which is of non-class type 'int' .由于元素类型是intProfit[i]的类型是int ,这就是为什么您会收到错误消息: request for member 'push_back' in [...] which is of non-class type 'int'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM