C 模板未在Linux GCC上编译

C++ template not compiling on Linux GCC

本文关键字:GCC 编译 Linux      更新时间:2023-10-16

我有一块代码,可以在Microsoft Visual Studio上进行编译,但在Linux Eclipse GCC上不正确。

这是代码:

template <class KEY, class BASEMAP>
class CGenericKeyToPointerMap : public std::map<KEY, BASEMAP>
{
private:
    iterator        tmpSearchIterator;
};

汇编输出为:

Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test.d" -MT"src/test.o" -o "src/test.o" "../src/test.cpp"
../src/test.cpp:22:2: error: invalid use of template-name ‘std::iterator’ without an argument list
  iterator  tmpSearchIterator;
  ^~~~~~~~
../src/test.cpp:22:2: note: class template argument deduction is only available with -std=c++17 or -std=gnu++17
In file included from /usr/include/c++/8/bits/stl_algobase.h:65,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from ../src/test.cpp:9:
/usr/include/c++/8/bits/stl_iterator_base_types.h:118:12: note: ‘template<class _Category, class _Tp, class _Distance, class _Pointer, class _Reference> struct std::iterator’ declared here
     struct iterator

避免使用

的另一个原因
using namespace std;

我猜您想在行中使用std::map<KEY, BASEMAP>::iterator而不是std::iterator

iterator        tmpSearchIterator;

在这种情况下,使用

using iterator = typename std::map<KEY, BASEMAP>::iterator;
iterator        tmpSearchIterator;