这个“缺少模板参数”是什么意思? C++错误

发布于 2024-10-15 22:18:36 字数 1206 浏览 2 评论 0原文

啊,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-10-22 22:18:36

您还需要更改 MyClass::haskey 方法。

bool haskey(const string &s)
{
    typename MyClass<C>::iterator it = this->find(s);
    return (it != this->end());
}

此类行为的说明位于 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.

bool haskey(const string &s)
{
    typename MyClass<C>::iterator it = this->find(s);
    return (it != this->end());
}

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

赏烟花じ飞满天 2024-10-22 22:18:36
iterator it = find(s);
return (it != end());

这条线应该是,

#ifdef WITH_TEMPLATE
           typename map<string, C>::iterator it = this->find(s);
           return (it != this->end()); 
#else
           map<string, int>::iterator it = find(s);
           return (it != end());
#endif
iterator it = find(s);
return (it != end());

This line should be as,

#ifdef WITH_TEMPLATE
           typename map<string, C>::iterator it = this->find(s);
           return (it != this->end()); 
#else
           map<string, int>::iterator it = find(s);
           return (it != end());
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文