繁体   English   中英

错误:“at”没有依赖于模板参数的参数,因此at的声明必须可用

[英]error: there are no arguments to 'at' that depend on a template parameter, so a declaration of at must be available

Noob在这里,

我正在尝试从Bjarne Stroustrup的'The C ++ Programming Language'编译这段代码,但CodeBlocks一直在向我抛出这个错误。

代码是关于范围检查向量函数中保存的数组。

这是代码:

#include <iostream>
#include <vector>
#include <array>

using namespace std;

int i = 1000;

template<class T> class Vec : public vector<T>
{
public:
    Vec() : vector<T>() { }

    T& operator[] (int i) {return at(i); }
    const T& operator[] (int i) const {return at(i); }
    //The at() operation is a vector subscript operation 
    //that throws an exception of type out_of_range
    //if its argument is out of the vector's range.
};

Vec<Entry> phone_book(1000);

int main()
{

    return 0;
}

返回的错误是:

  • 'at'没有依赖于模板参数的参数,因此'at'的声明必须可用
  • 注意:(如果使用'-fpermissive',G ++将接受您的代码,但不允许使用未声明的名称
  • 在成员函数'const T&operator [](int i)const'中:
  • 'at'没有依赖于模板参数的参数,因此'at'的声明必须可用
  • 在此范围内未声明“进入”
  • 模板参数1无效
  • '('标记)之前的声明中的无效类型

谁可以给我解释一下这个?

另外,如果我不使用'using namespace std;',我将如何实现呢?

替换at vector<T>::atthis->at

有关如何在模板中查找函数的规则现在比最初设计C ++时更严格。

现在,只有你这样做才能查找依赖库中的方法 - this-> ,否则它被认为是一个全局函数(或一个非依赖的基类/类本地/等)。

这有助于避免在实践中出现令人讨厌的意外,您认为方法调用变为全局调用,或者全局调用成为方法调用。 它还允许更早地检查模板方法体。

除了Yakk的答案,另一种解决方案是添加

using vector<T>::at;

Vec基本上将它添加到查找功能列表中。

这样, at()可以像往常一样使用,而不用基类类型或this->作为前缀。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM