问题描述
我尝试执行以下操作:
template <class T>
std::ifstream& operator>> (std::ifstream& fin, List<T> l)
{
T temp;
l.resize(0);
fin >> ignore(1,'\t');
for(ListIterator<T> i=l.begin();i!=l.end();i++)
{
fin >> ignore(1,'\t') >> temp;
l.push_back(temp);
}
return fin;
}
我必须读取文件中的所有内容。每个字段由'\t'
字符分隔,因此我必须忽略'\t'
字符
I have to read all the contents from a file. Each field is separated by '\t'
character, so I have to ignore the '\t'
characters.
错误日志如下:
/home/ramy/Documents/C++/Prova/Util.h||In function ‘std::ifstream& Util::operator>> (std::ifstream&, Util::List<T>)’:|
/home/ramy/Documents/C++/Prova/Util.h|431|error: there are no arguments to ‘ignore’ that depend on a template parameter, so a declaration of ‘ignore’ must be available|
/home/ramy/Documents/C++/Prova/Util.h|431|note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)|
||=== Build finished: 1 errors, 0 warnings ===|
推荐答案
对于内置类型,。依赖于模板参数的名称,例如
The error message comes up like this because in templates, you also enter the realm of dependent names and Two Phase Lookup. Names that depend on a template parameter, e.g.
template <typename T> void foo() {
T x;
x.frobnicate();
}
在阶段2中查找,不依赖于模板参数的名称,例如
are looked up in phase 2, which is upon instantiation. Names that do not depend on template parameters, like
class Foo {};
template <typename T> void foo() {
Foo foo;
foo.frobnicate();
}
在第一阶段必须是可解析的。
must be resolvable in the first phase.
这种分离功能可以帮助模板作者更早地找到错误,并找到正确的符号,这有助于使模板更通用。例如,在C#泛型中,一切都必须是可解析的,这对它们的灵活性提出了相当严格的限制(因为 必须由通用定义)。相反,一些旧的C ++编译器仅在阶段2中解析,即在实例化时间,其对于查找和错误发现具有一些微妙的结果。
This separation helps template authors to find bugs earlier and to find correct symbols, and it helps making templates more generic. For example, in C# generics, everything must be resolvable, which puts rather stringent limits on their flexibility (because everything that may be used by a generic must be defined). Oppositely, some old C++ compilers resolved in phase 2 only, i.e. at instantiation time, which had some subtle consequences for lookup and error finding.
C ++ 2阶段模型结合了最好的热切模型(C#)和延迟模型(一些旧的C ++编译器)。
The C++ 2-phase model combines the best of the eager-model (C#) and the lazy-model (some old C++ compilers).
这篇关于没有依赖于模板参数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!