我对 C++ 的使用有什么问题吗?标准库的发现?

发布于 2024-11-02 07:49:58 字数 2086 浏览 0 评论 0原文

我正在尝试使用 C++ 标准库的 find 算法,如下所示:

  template<class T>
  const unsigned int AdjacencyList<T>::_index_for_node(
      const std::vector<T>& list, const T& node
  ) throw(NoSuchNodeException)
  {
    std::vector<T>::iterator iter = std::find(list.begin(), list.end(), node);
  }

当我尝试编译时,出现以下错误:

In file included from ../AdjacencyList.cpp:8:
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&)’:
../AdjacencyList.h:99: error: expected ‘;’ before ‘iter’
../AdjacencyList.h:100: error: ‘iter’ was not declared in this scope
In file included from ../AdjacencyListTest.cpp:9:
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&)’:
../AdjacencyList.h:99: error: expected ‘;’ before ‘iter’
../AdjacencyList.h:100: error: ‘iter’ was not declared in this scope
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&) [with T = int]’:
../AdjacencyList.h:91:   instantiated from ‘const std::vector<T, std::allocator<_Tp1> > Graph::AdjacencyList<T>::neighbours(const T&) [with T = int]’
../AdjacencyListTest.cpp:18:   instantiated from here
../AdjacencyList.h:99: error: dependent-name ‘std::vector::iterator’ is parsed as a non-type, but instantiation yields a type
../AdjacencyList.h:99: note: say ‘typename std::vector::iterator’ if a type is meant

我感觉像“dependent-name 'std::vector::iterator'被解析为非类型,但实例化产生类型”位是理解我做错了什么的关键,但我的豌豆大脑无法提取含义。

更新:我需要根据接受的答案添加一个typename,并且还使用const_iterator,因此有问题的代码行变成:

    typename std::vector<T>::const_iterator iter = std::find(list.begin(), list.end(), node);

I'm trying to use the C++ standard library's find algorithm like this:

  template<class T>
  const unsigned int AdjacencyList<T>::_index_for_node(
      const std::vector<T>& list, const T& node
  ) throw(NoSuchNodeException)
  {
    std::vector<T>::iterator iter = std::find(list.begin(), list.end(), node);
  }

When I try to compile, I get these errors:

In file included from ../AdjacencyList.cpp:8:
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&)’:
../AdjacencyList.h:99: error: expected ‘;’ before ‘iter’
../AdjacencyList.h:100: error: ‘iter’ was not declared in this scope
In file included from ../AdjacencyListTest.cpp:9:
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&)’:
../AdjacencyList.h:99: error: expected ‘;’ before ‘iter’
../AdjacencyList.h:100: error: ‘iter’ was not declared in this scope
../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&) [with T = int]’:
../AdjacencyList.h:91:   instantiated from ‘const std::vector<T, std::allocator<_Tp1> > Graph::AdjacencyList<T>::neighbours(const T&) [with T = int]’
../AdjacencyListTest.cpp:18:   instantiated from here
../AdjacencyList.h:99: error: dependent-name ‘std::vector::iterator’ is parsed as a non-type, but instantiation yields a type
../AdjacencyList.h:99: note: say ‘typename std::vector::iterator’ if a type is meant

I feel like the "dependent-name ‘std::vector::iterator’ is parsed as a non-type, but instantiation yields a type" bit holds the key to understanding what I'm doing wrong, but my pea-brain cannot extract the meaning.

Update: I needed to add a typename, as per the accepted answer, and also use a const_iterator, so the problematic line of code became:

    typename std::vector<T>::const_iterator iter = std::find(list.begin(), list.end(), node);

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

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

发布评论

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

评论(1

北渚 2024-11-09 07:49:58
std::vector<T>::iterator iter = /* .... */; 

iterator 是一个从属名称(实际上,它取决于类型参数T)。除非您使用 typename,否则假定从属名称不会命名类型:

typename std::vector<T>::iterator iter = /* .... */;

有关更多信息,请考虑阅读 Stack Overflow C++ 常见问题解答条目 “我必须在何处以及为什么必须将“template”和“typename”放在依赖名称上?”

您还需要使用 const_iterator,因为 list 是 const 限定的。您可能还应该删除异常规范;最好“永远不要编写异常规范。”

std::vector<T>::iterator iter = /* .... */; 

iterator is a dependent name (effectively, it depends on the type parameter T). Dependent names are assumed not to name types unless you use typename:

typename std::vector<T>::iterator iter = /* .... */;

For more, consider reading the Stack Overflow C++ FAQ entry "Where and why do I have to put “template” and “typename” on dependent names?"

You will also need to use const_iterator, since list is const-qualified. You should probably drop the exception specification as well; it's best to "never write an exception specification."

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