类模板的成员函数返回该类中定义的类型的正确语法是什么?

发布于 2024-10-25 12:26:47 字数 1568 浏览 1 评论 0原文

我目前正在学习 Accelerated C++,但我陷入了练习 11-6。这个想法是将标准库向量重新实现为一个名为 Vec 的类。

我在使用迭代器擦除(迭代器)成员时遇到了问题,因为我不知道在类定义之外的正确语法,并且我尝试过的所有操作都会导致编译器错误。我目前拥有的代码是:

template <class T> T* Vec<T>::erase(T* pos){

    if(pos < avail)
        std::copy(pos + 1, avail, pos);

    --avail;
    alloc.destroy(avail);

    return pos;

}

这完美地工作。但是,出于可维护性目的以及与 stl 中算法的兼容性,我知道我应该这样做:

template <class T> Vec<T>::iterator Vec<T>::erase(Vec<T>::iterator pos){

    // As before

}

我已经在类定义中定义了迭代器,如下所示:

 typedef T* iterator;

尝试使用第二个进行编译代码片段结果:

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

遗憾的是,该警告对我来说没有多大意义,并且在阅读 它的 MSDN 页面 我无法确切地看到它如何应用于我的代码。

其余消息看起来好像编译器无法识别返回类型。

我尝试了许多不同的组合,但搜索并没有多大帮助。我将不胜感激任何帮助。谢谢!

I am working through Accelerated C++ at the moment, and I am stuck on exercise 11-6. The idea is to reimplement the standard library vector as a class called Vec.

I am having trouble with the iterator erase(iterator) member, because I don't know the correct syntax when outside of the class definition, and everything I've tried results in a compiler error. The code I currently have is:

template <class T> T* Vec<T>::erase(T* pos){

    if(pos < avail)
        std::copy(pos + 1, avail, pos);

    --avail;
    alloc.destroy(avail);

    return pos;

}

This works perfectly. However, for maintainability purposes and compatibility with algorithms in the stl, I know I should be doing something like this:

template <class T> Vec<T>::iterator Vec<T>::erase(Vec<T>::iterator pos){

    // As before

}

I've already defined iterator in the class definition, as follows:

 typedef T* iterator;

Trying to compile with the second code snippet results in:

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

Sadly, the warning doesn't make much sense to me, and after reading the MSDN page for it I can't exacly see how it would apply to my code.

The rest of the messages appear as if the compiler didn't recognise the return type.

I've tried many different combinations and searching hasn't been very helpful. I would appreciate any help. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2024-11-01 12:26:47
  template <class T> 
  typename Vec<T>::iterator Vec<T>::erase(typename Vec<T>::iterator pos){
 //^^^^^^^^ note this                      ^^^^^^^^ note this as well!

       //your code!
  }

也就是说,有两个地方需要 typename。这是因为 iterator 是一个依赖名称,所以 typename 是必需的!

请阅读由 Johannes 的好人在 Stackoverflow 上所作的精彩解释:

阅读完后,您还可以阅读此主题:


其他好文章的链接:

  template <class T> 
  typename Vec<T>::iterator Vec<T>::erase(typename Vec<T>::iterator pos){
 //^^^^^^^^ note this                      ^^^^^^^^ note this as well!

       //your code!
  }

That is, typename is required at two places. It's because iterator is a dependent name, so typename is required!

Read this excellent explanation by a great guy called Johannes, on Stackoverflow itself:

After reading that you can also read this topic:


Links to other good articles:

小糖芽 2024-11-01 12:26:47
template <class T> typename Vec<T>::iterator Vec<T>::erase(typename Vec<T>::iterator pos){

    // As before

}

...因为这里迭代器的名称​​依赖于模板参数”,没有typename编译器假装认为 Vec::iterator 是 Vec 的静态成员:)

template <class T> typename Vec<T>::iterator Vec<T>::erase(typename Vec<T>::iterator pos){

    // As before

}

... because iterator here is the name dependent on template parameter" without typename compiler pretends to think that Vec::iterator is a static member of Vec :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文