首页 > 解决方案 > C++ 模板未在 Linux GCC 上编译

问题描述

我有一段代码在 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

标签: c++gcc

解决方案


避免使用的另一个原因

using namespace std;

我猜你想使用std::map<KEY, BASEMAP>::iterator而不是std::iterator排队

iterator        tmpSearchIterator;

在这种情况下,使用

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

推荐阅读