简体   繁体   English

将模板添加到类中,该模板正在多集合中使用

[英]Adding a template to a class, that's being used in a multiset

The fix for this is probably very simple, but I cannot find the answer. 修复方法可能很简单,但是我找不到答案。 An answer will be rewarded with my thanks and my tears. 我的感谢和泪水将给您一个答案。

Basically the below code works just fine without the template (with the T's as primitives of course) but once I add the template, it says my argument list is missing. 基本上,下面的代码在没有模板的情况下也可以正常工作(当然以T为基元),但是一旦我添加了模板,它就会说我的参数列表丢失了。 I figured this has to do with declaring , but these functions are not using scope resolution, at least not in a way I'm familiar with. 我认为这与声明有关,但是这些函数没有使用范围解析,至少不是以我熟悉的方式。 How do I get this to work? 我该如何工作?

template<class T>
class Foo
{
public:
    Foo(T s, int i) : Data(s), pri(i) {}

    //Overload the relational operator so that the priority is compared.
    bool operator < (const Foo<T>& n) const { return n.pri < pri; }
    T getData() { return Data; }
    int getId() { return pri; }

private:
    T Data;
    int pri;
};

This is main 这是主要的

int main(void)
{
    set<Foo> s; //These should actually be multiset, but was trying to get it to
    s.insert(Foo("C++", 9)); //work as a set before jumping to multiset
    s.insert(Foo("Is ", 7));
    s.insert(Foo("Fun ", 3));

    set<Foo>::iterator p;
    for (p = s.begin(); p != s.end(); p++)
    {
        Foo n = *p;
        cout << "Id: " << n.getId() << "\t Data: " << n.getData() << endl;
    }

    return 0;
}

If you're curious, the program is supposed to take a string(or other type) along with a priority and sort the priority, the program isn't complete, but I'm supposed to use this Class in my program, but I'm tripped up over in converting it into a template. 如果您很好奇,该程序应该采用字符串(或其他类型)以及优先级并对优先级进行排序,则该程序并不完整,但是我应该在程序中使用此Class,但是在将其转换为模板时被绊倒了。

std::set expect a type . std::set期望为type

Before the introduction of the template part, Foo was a type. 在引入模板部分之前, Foo是一种类型。

Now that is a template class, Foo isn't a type anymore. 现在这是模板类, Foo不再是类型。 Foo<int> is a type; Foo<int>是一种类型; Foo<std::string> is a type; Foo<std::string>是一种类型; not Foo . 不是Foo

So 所以

std::set<Foo<int>> s;

can work, 能行得通,

std::set<Foo> s;

give an error. 给出一个错误。

When you declare class Foo {}; 当您声明class Foo {}; , Foo is the name of a class. Foo是类的名称。 However, when you declare template <class T> class Foo {}; 但是,当您声明template <class T> class Foo {}; , Foo is the name of a template. Foo模板的名称 A template is not a class: it's a recipe for creating classes. 模板不是类:它是创建类的秘诀。 Classes such as Foo<int> or Foo<char> . 诸如Foo<int>Foo<char>类的类。

It seems you want to store std::string s in your Foo s in the set, which means the code should look like this: 似乎您想将std::string存储在集合中的Foo中,这意味着代码应如下所示:

int main(void)
{
    set<Foo<string>> s; //These should actually be multiset, but was trying to get it to
    s.insert(Foo<string>("C++", 9)); //work as a set before jumping to multiset
    s.insert(Foo<string>("Is ", 7));
    s.insert(Foo<string>("Fun ", 3));

    set<Foo<string>>::iterator p;
    for (p = s.begin(); p != s.end(); p++)
    {
        Foo<string> n = *p;
        cout << "Id: " << n.getId() << "\t Data: " << n.getData() << endl;
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM