我目前正在通过Accelerated C++工作,我坚持练习11-6.我们的想法是将标准库向量重新实现为一个名为的类Vec.
我遇到了这个iterator erase(iterator)成员的问题,因为我不知道在类定义之外的正确语法,并且我尝试过的所有内容都会导致编译错误.我目前的代码是:
template <class T> T* Vec<T>::erase(T* pos){
if(pos < avail)
std::copy(pos + 1, avail, pos);
--avail;
alloc.destroy(avail);
return pos;
}
Run Code Online (Sandbox Code Playgroud)
这非常有效.但是,出于可维护性目的以及与stl中的算法的兼容性,我知道我应该这样做:
template <class T> Vec<T>::iterator Vec<T>::erase(Vec<T>::iterator pos){
// As before
}
Run Code Online (Sandbox Code Playgroud)
我已经iterator在类定义中定义了如下:
typedef T* iterator;
Run Code Online (Sandbox Code Playgroud)
尝试使用第二个代码片段进行编译会导致:
D:\Documents\Programming\Accelerated C++\Chapter 11>cl /EHsc Vec.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
Vec.cpp
Vec.cpp(23) : warning C4346: 'Vec<T>::iterator' : dependent name is not a type prefix with 'typename' to indicate a type
Vec.cpp(23) : error C2143: syntax error : missing ';' before 'Vec<T>::erase'
Vec.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Vec.cpp(23) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Run Code Online (Sandbox Code Playgroud)
可悲的是,警告对我来说没有多大意义,在阅读MSDN页面之后,我无法看到它如何应用于我的代码.
其余消息显示为编译器无法识别返回类型.
我尝试了很多不同的组合,但搜索并不是很有帮助.我将不胜感激任何帮助.谢谢!
template <class T>
typename Vec<T>::iterator Vec<T>::erase(typename Vec<T>::iterator pos){
//^^^^^^^^ note this ^^^^^^^^ note this as well!
//your code!
}
Run Code Online (Sandbox Code Playgroud)
也就是说,typename需要在两个地方.这是因为iterator是一个从属名称,所以typename是必需的!
在Stackoverflow上阅读这个名叫约翰内斯的好人的精彩解释:
阅读完之后,您还可以阅读以下主题:
与其他好文章的链接: