错误:没有依赖模板参数的“ 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的 C ++编程语言中编译这段代码,但是CodeBlocks不断抛出此错误。 p>
代码是有关范围检查向量函数中保存的数组的。
这是代码:
#include< iostream>
#include< vector>
#include< array>
使用命名空间std;
int i = 1000;
template< class T> Vec类:公共向量T。
{
public:
Vec():vector< T>(){}
T& operator [](int i){返回(i); }
const T& operator [](int i)const {返回(i); }
// at()操作是向量下标操作
//如果参数超出向量的范围,则会抛出out_of_range
类型的异常。
};
Vec< Entry> phone_book(1000);
int main()
{
返回0;
}
返回的错误为:
- 没有依赖模板参数的'at'参数,因此'at'声明必须可用
- 注意:(如果您使用 -fpermissive,则G ++会接受您的代码,但不赞成使用未声明的名称
- 在成员函数'const T& operator [](int i)const中':
- 'at'没有依赖模板参数的参数,因此'at'声明必须可用
- '
- 模板参数1无效
- 声明中的无效类型在'('令牌 $ b前$ b
有人可以向我解释吗?
此外,如果我要不要使用'using namespace std;'
将 at
替换为 vector< T> :: at
或 this-> at
。
与最初设计C ++相比,现在在模板中查找功能的规则更加严格。
现在,仅当您查找依赖库中的方法时, this->
,否则假定它是全局函数(或非依赖性基类/本地类等)。
这可以帮助避免实践中令人讨厌的意外情况,在这种情况下,您认为方法调用变成了全局调用,或者全局调用变成了方法调用。它还允许较早检查模板方法主体。
Noob here,
I'm trying to compile this segment of code from Bjarne Stroustrup's 'The C++ Programming Language' but CodeBlocks keeps throwing me this error.
The code is about range checking an array held in a vector function.
Here is the code:
#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;
}
The errors returned are:
- there are no arguments to 'at' that depend on a template parameter, so a declaration of 'at' must be available
- note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated
- In member function 'const T& operator[] (int i) const':
- there are no arguments to 'at' that depend on a template parameter, so a declaration of 'at' must be available
- 'Entry' was not declared in this scope
- template argument 1 is invalid
- invalid type in declaration before '(' token
Can someone explain this to me?
Also, how would I implement this if I were to not use 'using namespace std;'
Replace at
with vector<T>::at
or this->at
.
Rules for how functions are looked up in templates are tighter now than when C++ was being originally designed.
Now, methods in dependent bases are only looked up if you this->
, otherwise it is assumed to be a global function (or a non-dependent base/class local/etc).
This can help avoid nasty surprises in practice, where what you thought was a method call becomes a global one, or a global call becomes a method call. It also allows earlier checking of template method bodies.
这篇关于错误:没有依赖模板参数的“ at”参数,因此at的声明必须可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!