C ++模板未在Linux GCC上编译

时间:2019-07-03 05:39:52

标签: c++ 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

1 个答案:

答案 0 :(得分:3)

避免使用

的另一个原因
using namespace std;

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

iterator        tmpSearchIterator;

在这种情况下,请使用

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