error: there are no arguments to 'at' that depend on a template parameter, so a declaration of at must be available
error: there are no arguments to 'at' that depend on a template parameter, so a declaration of at must be available
这里是菜鸟,
我正在尝试从 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' 的声明必须可用
- 'Entry' 未在此范围内声明
- 模板参数 1 无效
- '(' 标记前的声明类型无效
谁能给我解释一下?
此外,如果我不使用 'using namespace std;'
,我将如何实现它
将at
替换为vector<T>::at
或this->at
。
现在在模板中查找函数的规则比最初设计 C++ 时更严格。
现在,仅当您 this->
时才会查找依赖基中的方法,否则它被假定为全局函数(或非依赖 base/class local/etc)。
这有助于避免在实践中出现令人讨厌的意外情况,即您认为的方法调用变成了全局调用,或者全局调用变成了方法调用。它还允许更早地检查模板方法主体。
除了 Yakk 的回答之外,另一种解决方案是添加
using vector<T>::at;
到 Vec
基本上是将其添加到查找函数列表中。
这样,at()
可以像往常一样使用,而无需使用基本 class 类型或 this->
.
作为前缀
这里是菜鸟,
我正在尝试从 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' 的声明必须可用
- 'Entry' 未在此范围内声明
- 模板参数 1 无效
- '(' 标记前的声明类型无效
谁能给我解释一下?
此外,如果我不使用 'using namespace std;'
,我将如何实现它将at
替换为vector<T>::at
或this->at
。
现在在模板中查找函数的规则比最初设计 C++ 时更严格。
现在,仅当您 this->
时才会查找依赖基中的方法,否则它被假定为全局函数(或非依赖 base/class local/etc)。
这有助于避免在实践中出现令人讨厌的意外情况,即您认为的方法调用变成了全局调用,或者全局调用变成了方法调用。它还允许更早地检查模板方法主体。
除了 Yakk 的回答之外,另一种解决方案是添加
using vector<T>::at;
到 Vec
基本上是将其添加到查找函数列表中。
这样,at()
可以像往常一样使用,而无需使用基本 class 类型或 this->
.