[英]Error C++ : ‘const_iterator’ does not name a type;
I have a class called Nuage (which is a collection of Points) where I will use the functions of the class Vector to manipulate the class.我有一个名为 Nuage(它是 Points 的集合)的类,我将在其中使用 Vector 类的函数来操作该类。 The nuage.cpp file is: nuage.cpp 文件是:
#include <iostream>
#include "nuage.hpp"
template <typename T>
Nuage<T>::Nuage(){}
template <typename T>
void Nuage<T>::ajouter(const T& p) {
v.push_back(p);
}
template <typename T>
unsigned int Nuage<T>::size() const {
return v.size();
}
template <typename T>
const_iterator Nuage<T>::begin() const{
return v.begin();
template <typename T>
Nuage<T>::~Nuage(){}
nuage.hpp is: nuage.hpp 是:
#ifndef NUAGE_HPP
#define NUAGE_HPP
#include <cstdlib>
#include <sstream>
#include <vector>
template <typename T>
class Nuage {
private:
std::vector<T> v;
public:
using const_iterator = typename std::vector<T>::const_iterator;
Nuage();
void ajouter(const T&);
unsigned int size() const;
const_iterator begin() const;
~Nuage();
};
#endif
I have an error that says:我有一个错误说:
error: ‘const_iterator’ does not name a type; did you mean ‘constexpr’?
The error is in this line: const_iterator Nuage<T>::begin() const{
错误在这一行: const_iterator Nuage<T>::begin() const{
Can someone help me to find it?有人可以帮我找到它吗? THanks谢谢
Use用
template <typename T>
typename Nuage<T>::const_iterator Nuage<T>::begin() const{
return v.begin();
}
When the compiler works with your .cpp
file you reference a type const_iterator
which does not exist.当编译器处理您的.cpp
文件时,您引用了一个不存在的类型const_iterator
。 Only Nuage<T>::const_iterator
exists.只有Nuage<T>::const_iterator
存在。 Easy to oversee.易于监督。
The typename
keyword is needed beacuse Nuage<T>::const_iterator
is a dependent name.因为Nuage<T>::const_iterator
是依赖名称,所以需要typename
关键字。 The compiler needs to be told that this expression is a type, not a static variable or a function or something else inside the class.编译器需要被告知这个表达式是一种类型,而不是静态变量或函数或类中的其他东西。
const_iterator
exists only within the scope of instantiation of Nuage
for some T. You have two options to fix your code. const_iterator
仅存在于某些 T 的Nuage
实例化范围内。您有两种选择来修复您的代码。 Either add explicit Nuage<T>::
:添加显式Nuage<T>::
:
template <typename T>
typename Nuage<T>::const_iterator Nuage<T>::begin() const{
return v.begin();
}
or use postfix return type notation :或使用后缀返回类型表示法:
template <typename T>
auto Nuage<T>::begin() -> const_iterator const{
return v.begin();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.