这个“缺少模板参数”是什么意思? C++错误
啊,C++ 模板...
我看到的代码,
对我来说很有意义,
但海湾合作委员会...
它不同意。
以下代码按预期编译并运行,但如果取消注释 #define
,则会收到错误,我不明白。符号迭代器仍然只能引用一件事:超类中的 typedef。所以我想我有两个问题: 1.这些错误是什么意思? 2. 修复它们的最佳方法是什么。
#include <map>
#include <string>
#include <cstdio>
using namespace std;
// #define WITH_TEMPLATE 1
#ifdef WITH_TEMPLATE
template <class C>
struct MyClass : public map<string, C>
#else
struct MyClass : public map<string, int>
#endif
{
bool haskey(const string &s)
{
iterator it = find(s);
return (it != end());
}
};
int main()
{
#ifdef WITH_TEMPLATE
MyClass<int> m;
#else
MyClass m;
#endif
m["test"] = 10;
printf("%d %d\n", m.haskey("test"), m.haskey("no"));
}
来自海湾合作委员会的错误:
temp.cc:在成员函数 'bool MyClass::haskey(const std::string&)' 中:
temp.cc:18: 错误:在“it”之前缺少模板参数
temp.cc:18: 错误:预期为“;”在“它”之前
temp.cc:19: 错误:“it”未在此范围内声明
temp.cc:19: 错误:没有依赖于模板参数的“end”参数,因此必须提供“end”声明
temp.cc:19: 错误:(如果您使用“-fpermissive”,G++ 将接受您的代码,但不推荐使用未声明的名称)
Ahh, C++ templates...
The code I see,
makes sense to me,
but GCC...
it disagrees.
The following code compiles and runs as expected, but if you uncomment that #define
, you get the error, which I don't understand. The symbol iterator
still has only one thing it can refer to: the typedef in the superclass. So I guess I have two questions: 1. What do the errors mean? 2. What is the best way to fix them.
#include <map>
#include <string>
#include <cstdio>
using namespace std;
// #define WITH_TEMPLATE 1
#ifdef WITH_TEMPLATE
template <class C>
struct MyClass : public map<string, C>
#else
struct MyClass : public map<string, int>
#endif
{
bool haskey(const string &s)
{
iterator it = find(s);
return (it != end());
}
};
int main()
{
#ifdef WITH_TEMPLATE
MyClass<int> m;
#else
MyClass m;
#endif
m["test"] = 10;
printf("%d %d\n", m.haskey("test"), m.haskey("no"));
}
Errors from GCC:
temp.cc: In member function ‘bool MyClass::haskey(const std::string&)’:
temp.cc:18: error: missing template arguments before ‘it’
temp.cc:18: error: expected `;' before ‘it’
temp.cc:19: error: ‘it’ was not declared in this scope
temp.cc:19: error: there are no arguments to ‘end’ that depend on a template parameter, so a declaration of ‘end’ must be available
temp.cc:19: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还需要更改 MyClass::haskey 方法。
此类行为的说明位于 http://physical.ucsd.edu/students/courses/winter2008/physicals141/manuals/rhel-gcc-en-4/c-- -misunderstands.html(来自另一个答案的评论的链接,以防万一)。
整个固定示例代码: http://ideone.com/G7Rty
You need to change your MyClass::haskey method too.
Explanation of such a behavior is in section "Name lookup, templates, and accessing members of base classes" on http://physics.ucsd.edu/students/courses/winter2008/physics141/manuals/rhel-gcc-en-4/c---misunderstandings.html (link from another answer's comment, just in case).
Whole fixed example code: http://ideone.com/G7Rty
这条线应该是,
This line should be as,