c++模板问题

发布于 2024-10-31 05:17:52 字数 526 浏览 1 评论 0原文

我有一个类,它有一个用于其他目的的模板:

template<class t>
class MyClass {
    public: //of course public...
    t foo;
    std::string text;
}

我有另一个类,它的方法通过参数获取所有这些类,并希望将指针存储在数组中。该类不想访问类的特定(临时)部分,而只想访问公共属性/方法。

class Container {
    public: //of course public...
    MyClass* array; //this is allocated with some magic.
    void bar(MyClass& m) {
      and want to store the class in a MyClass* array.
    }
}

这是模板参数列表缺失的错误,

我该如何解决这个问题?

i have a class which has a template by other purposes:

template<class t>
class MyClass {
    public: //of course public...
    t foo;
    std::string text;
}

and i have another class which method get all kind of these class through the arguments, and want to store the pointer in an array. The class dont want to access the specific (tempalted) parts of the classes only the common attributes/methods.

class Container {
    public: //of course public...
    MyClass* array; //this is allocated with some magic.
    void bar(MyClass& m) {
      and want to store the class in a MyClass* array.
    }
}

here is the error that argument list for template missing

how can i solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

棒棒糖 2024-11-07 05:17:52

最简单的方法是使该函数也成为模板:

template <class t>
void bar(MyClass<t>& m) {
    // ...
}

请注意,它可能应该是 const MyClass&,因为您不需要修改它。


你的新代码毫无意义。不存在 MyClass 类型的对象,因为 MyClass 是一个模板。如果您想对这些类进行操作而不考虑它们的模板参数,那么您需要将非模板部分分解为基类:

class MyClassBase
{
public:
    // polymorphic base classes should always have virtual destructors
    ~MyClassBase() {}

    virtual void some_function() = 0;
};

template <typename T>
class MyClass : public MyClassBase
{
public:
    // implement abstract functions
    void some_function()
    {
        // template argument T is available here
    }
};

然后您可以引用该基类,并且当您调用虚拟函数时,它将动态分派:

class Container
{
public:
    // no magic: use a std::vector for dynamic arrays
    std::vector<MyClassBase*> array; // not by value! avoid slicing

    void bar(MyClassBase& m)
    {
        array.push_back(&m);
    }

    void baz()
    {
        array[0]->some_function(); // for example
    }
};

The simplest method would be to make that function a template as well:

template <class t>
void bar(MyClass<t>& m) {
    // ...
}

Note that that should probably be const MyClass<t>&, because you don't need to modify it.


Your new code is meaningless. There is no such that as an object of type MyClass, because MyClass is a template. If you want to operate on these classes irrespective of their template argument, then you need to factor out the non-template portions as a base class:

class MyClassBase
{
public:
    // polymorphic base classes should always have virtual destructors
    ~MyClassBase() {}

    virtual void some_function() = 0;
};

template <typename T>
class MyClass : public MyClassBase
{
public:
    // implement abstract functions
    void some_function()
    {
        // template argument T is available here
    }
};

Then you can refer to that base, and when you call a virtual function it will dynamically dispatch:

class Container
{
public:
    // no magic: use a std::vector for dynamic arrays
    std::vector<MyClassBase*> array; // not by value! avoid slicing

    void bar(MyClassBase& m)
    {
        array.push_back(&m);
    }

    void baz()
    {
        array[0]->some_function(); // for example
    }
};
音盲 2024-11-07 05:17:52

放置一个公共基类怎么样?

class MyClassCommon {
protected:
    ~MyClassCommon() { }

public:
    std::string text;
};

template<class t>
class MyClass : public MyClassCommon {
public: // of course public...
    t foo;
};

class Container {
public: // of course public...
    MyClassCommon* array; // this is allocated with some magic.
    void bar(MyClassCommon& m) {
        /* ... */
    }
};

How about putting a common base class.

class MyClassCommon {
protected:
    ~MyClassCommon() { }

public:
    std::string text;
};

template<class t>
class MyClass : public MyClassCommon {
public: // of course public...
    t foo;
};

class Container {
public: // of course public...
    MyClassCommon* array; // this is allocated with some magic.
    void bar(MyClassCommon& m) {
        /* ... */
    }
};
陈甜 2024-11-07 05:17:52

如果要创建“多模板”数组,最好使用非模板类作为模板类的基类。或者您可以创建一个模板数组并在其中存储任何对象。

If you want to create a "multi-template" array, you'd better use a non-template class as a base class of a template class. Or you can make a template array and store any objects in it.

梦魇绽荼蘼 2024-11-07 05:17:52

类中的文本变量是私有的,因此除非您的 bar 函数是该类的方法,否则您不能像这样合法地使用它

the text variable in your class is private so unless you bar function is a method of the class you can't legally use it like that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文