对大量 C++ 错误输出完全无能为力。我认为这与预定义函数和迭代器有关

Completely clueless on massive c++ error output. I think it has to do with predefined functions and iterator

代码如下:

#include <vector>
#include <string>
#include <algorithm>
#include "inventory.h"
#include <sstream>
#include <iostream>

using namespace std;

    Inventory::Inventory () {
    }
    bool Inventory::NameSort(vector<string> word1, vector<string> word2) {
        return word1[0].compare(word2[0]) <= 0;
    }
    bool Inventory::IntSort(vector<string> int1, vector<string> int2) {
        int first = atoi(int1[1].c_str());
        int second = atoi(int2[1].c_str());
        return first <= second;
    }
    int Inventory::vectcontains(string item) {
        for(unsigned int i = 0; i < inventory.size(); i++) {
            if(inventory[i][0].compare(item) == 0) {
                return i;
            }
        }
        return -1;
    }
    void Inventory::Update (string item, int amount) {
        int contains = vectcontains(item);
        if( contains < 0) {
            stringstream ss;
            ss << amount;
            vector<string> adding (item, ss.str());
            inventory.push_back(adding);
        }
        else {
            int old = atoi((inventory[contains][1]).c_str());
            stringstream stst;
            stst << (amount + old);
            string put = stst.str();
            inventory[contains].at(1) = put;
        }
    }
    void Inventory::ListByName () {
        sort(inventory.begin(), inventory.end(), NameSort);
        for (unsigned int i = 0; i < inventory.size(); i++) {
            cout << inventory[i][0] << inventory[i][1] << endl;
        }
    }
    void Inventory::ListByQuantity () {
        sort(inventory.begin(), inventory.end(), IntSort);
        for(unsigned int i = 0; i < inventory.size(); i++) {
            cout << inventory[i][0] << inventory[i][1] << endl;
        }
    }

这是输出:

$ g++ -g -Wall inventory.cpp
In file included from c:\mingw\include\c++.9.1\bits\stl_algobase.h:65:0,
                 from c:\mingw\include\c++.9.1\vector:60,
                 from inventory.cpp:1:
c:\mingw\include\c++.9.1\bits\stl_iterator_base_types.h: In instantiation of '
struct std::iterator_traits<std::basic_string<char> >':
c:\mingw\include\c++.9.1\bits\stl_vector.h:1262:24:   required from 'void std:
:vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std
::__false_type) [with _InputIterator = std::basic_string<char>; _Tp = std::basic
_string<char>; _Alloc = std::allocator<std::basic_string<char> >]'
c:\mingw\include\c++.9.1\bits\stl_vector.h:413:55:   required from 'std::vecto
r<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [w
ith _InputIterator = std::basic_string<char>; _Tp = std::basic_string<char>; _Al
loc = std::allocator<std::basic_string<char> >; std::vector<_Tp, _Alloc>::alloca
tor_type = std::allocator<std::basic_string<char> >]'
inventory.cpp:33:50:   required from here
c:\mingw\include\c++.9.1\bits\stl_iterator_base_types.h:165:53: error: no type
 named 'iterator_category' in 'class std::basic_string<char>'
       typedef typename _Iterator::iterator_category iterator_category;
                                                     ^
In file included from c:\mingw\include\c++.9.1\bits\stl_algobase.h:71:0,
                 from c:\mingw\include\c++.9.1\vector:60,
                 from inventory.cpp:1:
c:\mingw\include\c++.9.1\bits\predefined_ops.h: In instantiation of 'bool __gn
u_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [wit
h _Iterator1 = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char>
>*, std::vector<std::vector<std::basic_string<char> > > >; _Iterator2 = __gnu_cx
x::__normal_iterator<std::vector<std::basic_string<char> >*, std::vector<std::ve
ctor<std::basic_string<char> > > >; _Compare = bool (Inventory::*)(std::vector<s
td::basic_string<char> >, std::vector<std::basic_string<char> >)]':
c:\mingw\include\c++.9.1\bits\stl_algo.h:1846:27:   required from 'void std::_
_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _R
andomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string
<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Compare = __g
nu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_string
<char> >, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1884:70:   required from 'void std::_
_final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [w
ith _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_
string<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Compare
 = __gnu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_
string<char> >, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1970:55:   required from 'void std::_
_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAcces
sIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*,
 std::vector<std::vector<std::basic_string<char> > > >; _Compare = __gnu_cxx::__
ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_string<char> >,
std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:4707:78:   required from 'void std::s
ort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std
::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<c
har> > > >; _Compare = bool (Inventory::*)(std::vector<std::basic_string<char> >
, std::vector<std::basic_string<char> >)]'
inventory.cpp:45:58:   required from here
c:\mingw\include\c++.9.1\bits\predefined_ops.h:121:46: error: must use '.*' or
 '->*' to call pointer-to-member function in '((__gnu_cxx::__ops::_Iter_comp_ite
r<bool (Inventory::*)(std::vector<std::basic_string<char> >, std::vector<std::ba
sic_string<char> >)>*)this)->__gnu_cxx::__ops::_Iter_comp_iter<bool (Inventory::
*)(std::vector<std::basic_string<char> >, std::vector<std::basic_string<char> >)
>::_M_comp (...)', e.g. '(... ->* ((__gnu_cxx::__ops::_Iter_comp_iter<bool (Inve
ntory::*)(std::vector<std::basic_string<char> >, std::vector<std::basic_string<c
har> >)>*)this)->__gnu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vect
or<std::basic_string<char> >, std::vector<std::basic_string<char> >)>::_M_comp)
(...)'
         { return bool(_M_comp(*__it1, *__it2)); }
                                              ^
c:\mingw\include\c++.9.1\bits\predefined_ops.h: In instantiation of 'bool __gn
u_cxx::__ops::_Val_comp_iter<_Compare>::operator()(_Value&, _Iterator) [with _Va
lue = std::vector<std::basic_string<char> >; _Iterator = __gnu_cxx::__normal_ite
rator<std::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic
_string<char> > > >; _Compare = bool (Inventory::*)(std::vector<std::basic_strin
g<char> >, std::vector<std::basic_string<char> >)]':
c:\mingw\include\c++.9.1\bits\stl_algo.h:1827:34:   required from 'void std::_
_unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIte
rator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, std
::vector<std::vector<std::basic_string<char> > > >; _Compare = __gnu_cxx::__ops:
:_Val_comp_iter<bool (Inventory::*)(std::vector<std::basic_string<char> >, std::
vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1855:46:   required from 'void std::_
_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _R
andomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string
<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Compare = __g
nu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_string
<char> >, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1884:70:   required from 'void std::_
_final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [w
ith _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_
string<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Compare
 = __gnu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_
string<char> >, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1970:55:   required from 'void std::_
_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAcces
sIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*,
 std::vector<std::vector<std::basic_string<char> > > >; _Compare = __gnu_cxx::__
ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_string<char> >,
std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:4707:78:   required from 'void std::s
ort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std
::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<c
har> > > >; _Compare = bool (Inventory::*)(std::vector<std::basic_string<char> >
, std::vector<std::basic_string<char> >)]'
inventory.cpp:45:58:   required from here
c:\mingw\include\c++.9.1\bits\predefined_ops.h:166:37: error: must use '.*' or
 '->*' to call pointer-to-member function in '((__gnu_cxx::__ops::_Val_comp_iter
<bool (Inventory::*)(std::vector<std::basic_string<char> >, std::vector<std::bas
ic_string<char> >)>*)this)->__gnu_cxx::__ops::_Val_comp_iter<bool (Inventory::*)
(std::vector<std::basic_string<char> >, std::vector<std::basic_string<char> >)>:
:_M_comp (...)', e.g. '(... ->* ((__gnu_cxx::__ops::_Val_comp_iter<bool (Invento
ry::*)(std::vector<std::basic_string<char> >, std::vector<std::basic_string<char
> >)>*)this)->__gnu_cxx::__ops::_Val_comp_iter<bool (Inventory::*)(std::vector<s
td::basic_string<char> >, std::vector<std::basic_string<char> >)>::_M_comp) (...
)'
  { return bool(_M_comp(__val, *__it)); }
                                     ^
c:\mingw\include\c++.9.1\bits\predefined_ops.h: In instantiation of 'bool __gn
u_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _It
erator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*, st
d::vector<std::vector<std::basic_string<char> > > >; _Value = std::vector<std::b
asic_string<char> >; _Compare = bool (Inventory::*)(std::vector<std::basic_strin
g<char> >, std::vector<std::basic_string<char> >)]':
c:\mingw\include\c++.9.1\bits\stl_heap.h:129:76:   required from 'void std::__
push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _Ran
domAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<c
har> >*, std::vector<std::vector<std::basic_string<char> > > >; _Distance = long
 long int; _Tp = std::vector<std::basic_string<char> >; _Compare = __gnu_cxx::__
ops::_Iter_comp_val<bool (Inventory::*)(std::vector<std::basic_string<char> >, s
td::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_heap.h:230:51:   required from 'void std::__
adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _R
andomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string
<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Distance = lo
ng long int; _Tp = std::vector<std::basic_string<char> >; _Compare = __gnu_cxx::
__ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_string<char> >
, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_heap.h:334:15:   required from 'void std::__
make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomA
ccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char>
 >*, std::vector<std::vector<std::basic_string<char> > > >; _Compare = __gnu_cxx
::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_string<char>
 >, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1673:49:   required from 'void std::_
_heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator
, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vect
or<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char> >
 > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vect
or<std::basic_string<char> >, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1932:59:   required from 'void std::_
_partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterato
r, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vec
tor<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<char>
> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)(std::vec
tor<std::basic_string<char> >, std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1947:59:   required from 'void std::_
_introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [
with _RandomAccessIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic
_string<char> >*, std::vector<std::vector<std::basic_string<char> > > >; _Size =
 long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (Inventory::*)
(std::vector<std::basic_string<char> >, std::vector<std::basic_string<char> >)>]
'
c:\mingw\include\c++.9.1\bits\stl_algo.h:1969:11:   required from 'void std::_
_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAcces
sIterator = __gnu_cxx::__normal_iterator<std::vector<std::basic_string<char> >*,
 std::vector<std::vector<std::basic_string<char> > > >; _Compare = __gnu_cxx::__
ops::_Iter_comp_iter<bool (Inventory::*)(std::vector<std::basic_string<char> >,
std::vector<std::basic_string<char> >)>]'
c:\mingw\include\c++.9.1\bits\stl_algo.h:4707:78:   required from 'void std::s
ort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<std
::vector<std::basic_string<char> >*, std::vector<std::vector<std::basic_string<c
har> > > >; _Compare = bool (Inventory::*)(std::vector<std::basic_string<char> >
, std::vector<std::basic_string<char> >)]'
inventory.cpp:45:58:   required from here
c:\mingw\include\c++.9.1\bits\predefined_ops.h:141:37: error: must use '.*' or
 '->*' to call pointer-to-member function in '((__gnu_cxx::__ops::_Iter_comp_val
<bool (Inventory::*)(std::vector<std::basic_string<char> >, std::vector<std::bas
ic_string<char> >)>*)this)->__gnu_cxx::__ops::_Iter_comp_val<bool (Inventory::*)
(std::vector<std::basic_string<char> >, std::vector<std::basic_string<char> >)>:
:_M_comp (...)', e.g. '(... ->* ((__gnu_cxx::__ops::_Iter_comp_val<bool (Invento
ry::*)(std::vector<std::basic_string<char> >, std::vector<std::basic_string<char
> >)>*)this)->__gnu_cxx::__ops::_Iter_comp_val<bool (Inventory::*)(std::vector<s
td::basic_string<char> >, std::vector<std::basic_string<char> >)>::_M_comp) (...
)'
  { return bool(_M_comp(*__it, __val)); }
                                     ^

我试过寻找答案,但错误太大了。不可能将其归结为可搜索的内容。我不确定会出现什么问题,因为我不使用 vector 中的迭代方法。我唯一的猜测是我的算法排序方法可能是问题所在。

库存:

vector<vector<string> > inventory;

你不能使用非static成员函数作为std::sort的参数,就像你有:

sort(inventory.begin(), inventory.end(), NameSort);

创建NameSortIntSortstatic成员函数或非成员函数。

同时,将这些函数的参数类型更改为 std::vector<std::string> const&。否则,您将制作许多不必要的 std::vector<std::string> 副本。

不过,我感觉inventorystd::vector<std::string>>类型的。在那种情况下,这些参数需要是 std::string const&.

类型