gcc中继承std:vector类的有关问题
gcc中继承std::vector类的问题
以下代码在VS2005中编译通过,在gcc中却遇到了问题
(其中继承vector的目的主要是为了便于日后改用其他容器。)
gcc报告错误
Test.cpp:10:2: 错误: expected class-name before ‘{’ token
Test.cpp:13:21: 错误: expected ‘)’ before ‘iter’
Test.cpp:16:2: 错误: expected class-name before ‘{’ token
Test.cpp:19:32: 错误: expected ‘)’ before ‘iter’
Test.cpp:22:2: 错误: expected class-name before ‘{’ token
Test.cpp:25:36: 错误: expected ‘)’ before ‘iter’
Test.cpp: 在成员函数‘bool Vector<_Type>::Empty()’中:
Test.cpp:28:30: 错误: ‘empty’的实参不依赖模板参数,所以‘empty’的声明必须可用 [-fpermissive]
Test.cpp:28:30: 附注: (如果您使用‘-fpermissive’,G++ 会接受您的代码,但是允许使用未定义的名称是不建议使用的风格)
Test.cpp: 在成员函数‘unsigned int Vector<_Type>::Size()’中:
Test.cpp:29:36: 错误: ‘size’的实参不依赖模板参数,所以‘size’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::Iterator Vector<_Type>::Begin()’中:
Test.cpp:32:34: 错误: ‘begin’的实参不依赖模板参数,所以‘begin’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::Iterator Vector<_Type>::End()’中:
Test.cpp:33:30: 错误: ‘end’的实参不依赖模板参数,所以‘end’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ConstIterator Vector<_Type>::Begin() const’中:
Test.cpp:34:45: 错误: ‘begin’的实参不依赖模板参数,所以‘begin’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ConstIterator Vector<_Type>::End() const’中:
Test.cpp:35:41: 错误: ‘end’的实参不依赖模板参数,所以‘end’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ReverseIterator Vector<_Type>::ReverseBegin()’中:
Test.cpp:36:49: 错误: ‘rbegin’的实参不依赖模板参数,所以‘rbegin’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ReverseIterator Vector<_Type>::ReverseEnd()’中:
Test.cpp:37:45: 错误: ‘rend’的实参不依赖模板参数,所以‘rend’的声明必须可用 [-fpermissive]
请问应该如何解决?不胜感激。
以下代码在VS2005中编译通过,在gcc中却遇到了问题
(其中继承vector的目的主要是为了便于日后改用其他容器。)
- C/C++ code
//Test.cpp #include <vector> template <typename _Type> class Vector : public std::vector<_Type> { public: class Iterator : public iterator { public: Iterator() {}; Iterator(iterator iter) : iterator(iter) {}; }; class ConstIterator : public const_iterator { public: ConstIterator() {}; ConstIterator(const_iterator iter) : const_iterator(iter) {}; }; class ReverseIterator : public reverse_iterator { public: ReverseIterator() {}; ReverseIterator(reverse_iterator iter) : reverse_iterator(iter) {}; }; virtual ~Vector() {}; bool Empty() { return empty(); }; unsigned int Size() { return size(); }; void Append(_Type element) { push_back(element); }; Iterator Remove(Iterator iter) { return erase(iter); }; Iterator Begin() { return begin(); }; Iterator End() { return end(); }; ConstIterator Begin() const { return begin(); }; ConstIterator End() const { return end(); }; ReverseIterator ReverseBegin() { return rbegin(); }; ReverseIterator ReverseEnd() { return rend(); }; }; int main(int argc,char *argv[]) { return 0; }
gcc报告错误
Test.cpp:10:2: 错误: expected class-name before ‘{’ token
Test.cpp:13:21: 错误: expected ‘)’ before ‘iter’
Test.cpp:16:2: 错误: expected class-name before ‘{’ token
Test.cpp:19:32: 错误: expected ‘)’ before ‘iter’
Test.cpp:22:2: 错误: expected class-name before ‘{’ token
Test.cpp:25:36: 错误: expected ‘)’ before ‘iter’
Test.cpp: 在成员函数‘bool Vector<_Type>::Empty()’中:
Test.cpp:28:30: 错误: ‘empty’的实参不依赖模板参数,所以‘empty’的声明必须可用 [-fpermissive]
Test.cpp:28:30: 附注: (如果您使用‘-fpermissive’,G++ 会接受您的代码,但是允许使用未定义的名称是不建议使用的风格)
Test.cpp: 在成员函数‘unsigned int Vector<_Type>::Size()’中:
Test.cpp:29:36: 错误: ‘size’的实参不依赖模板参数,所以‘size’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::Iterator Vector<_Type>::Begin()’中:
Test.cpp:32:34: 错误: ‘begin’的实参不依赖模板参数,所以‘begin’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::Iterator Vector<_Type>::End()’中:
Test.cpp:33:30: 错误: ‘end’的实参不依赖模板参数,所以‘end’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ConstIterator Vector<_Type>::Begin() const’中:
Test.cpp:34:45: 错误: ‘begin’的实参不依赖模板参数,所以‘begin’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ConstIterator Vector<_Type>::End() const’中:
Test.cpp:35:41: 错误: ‘end’的实参不依赖模板参数,所以‘end’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ReverseIterator Vector<_Type>::ReverseBegin()’中:
Test.cpp:36:49: 错误: ‘rbegin’的实参不依赖模板参数,所以‘rbegin’的声明必须可用 [-fpermissive]
Test.cpp: 在成员函数‘Vector<_Type>::ReverseIterator Vector<_Type>::ReverseEnd()’中:
Test.cpp:37:45: 错误: ‘rend’的实参不依赖模板参数,所以‘rend’的声明必须可用 [-fpermissive]
请问应该如何解决?不胜感激。