虚拟模板?

发布于 2024-10-25 15:05:54 字数 471 浏览 1 评论 0原文

我试图声明一个抽象类,但只是模板化虚函数的行为就会让编译器抱怨。这通常是如何实现的?例如,在我的头文件中,

virtual SpecialList<Point> *getPoints() const;

编译器声明“ISO C++ 禁止声明没有类型的‘SpecialList’”。

编辑 Point 和 SpecialList 都包含在此类的定义中。作为一个更详细的例子,

// SomeClass.h
#include "SpecialList.h"
#include "Point.h"

class SomeClass
{
public:
    SomeClass();
    virtual SpecialList<Point> *getPoints() const;
//snip
};

仍然没有解决..

I am trying to declare an abstract class, but just the act of templating a virtual function makes the compiler complain. How is this normally accomplished? For example, in my header file I have:

virtual SpecialList<Point> *getPoints() const;

To which the compiler states "ISO C++ forbids declaration of 'SpecialList' with no type."

edit
Both Point and SpecialList are included in the definition of this class. As a more verbose example,

// SomeClass.h
#include "SpecialList.h"
#include "Point.h"

class SomeClass
{
public:
    SomeClass();
    virtual SpecialList<Point> *getPoints() const;
//snip
};

Still not solved..

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

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

发布评论

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

评论(3

豆芽 2024-11-01 15:05:54
struct A
{
    virtual vector<int>* f() const = 0;
};

对我来说效果很好。确保在使用“SpecialList”和“Point”之前已定义它们。

struct A
{
    virtual vector<int>* f() const = 0;
};

Works fine for me. Make sure that 'SpecialList' and 'Point' are defined before you use them.

蛮可爱 2024-11-01 15:05:54

看起来您在使用它之前还没有定义Point

在抽象类之前定义Point,或者包含定义它的头文件!

--

或者,如果您尝试定义虚拟函数模板,如下所示:

template<typename Point>
virtual SpecialList<Point> *getPoints() const;

那么这是不可能的。不允许使用虚函数模板!

It looks like you haven't defined Point before you're using it.

Define Point before the abstract class, or include the header file in which it's defined!

--

Or in case if you're trying to define virtual function template, something like this:

template<typename Point>
virtual SpecialList<Point> *getPoints() const;

Then it's not possible. virtual function template is not allowed!

祁梦 2024-11-01 15:05:54

检查您是否#included SpecialList 和 Point 类声明或前向声明或类型定义它。

Check if you #included SpecialList and Point class declarations or forward-declarated or typedefed it.

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