具有用户定义类型向量的虚函数

发布于 2024-10-31 20:07:30 字数 780 浏览 3 评论 0原文

我想定义一个带有结构变量向量的抽象基类,以及一个由派生类实现的虚函数:

class TestFather {

private:
    struct myStruct
    {
        // struct definition
    };
    vector<myStruct> myStructVect;

public:
    virtual vector<myStruct> get_myStructVect() = 0;
};

但是当我编写派生类时:

#include "TestFather.h"

class TestSon : public TestFather{

private:
    struct myStruct
    {
        // struct definition
    };
    vector<myStruct> myStructVect;

public:
    vector<myStruct> get_myStructVect();
};

我收到此错误:

invalid covariant return type for ‘virtual std::vector<ProvaSon::myStruct, std::allocator<ProvaSon::myStruct> > ProvaSon::get_myStructVect()’

我做错了什么还是可能我正在尝试做一些语言禁止的事情?

I want to define an abstract base class with a vector of struct variables, and a virtual function to be implemented by derived classes:

class TestFather {

private:
    struct myStruct
    {
        // struct definition
    };
    vector<myStruct> myStructVect;

public:
    virtual vector<myStruct> get_myStructVect() = 0;
};

but when I write the derived class:

#include "TestFather.h"

class TestSon : public TestFather{

private:
    struct myStruct
    {
        // struct definition
    };
    vector<myStruct> myStructVect;

public:
    vector<myStruct> get_myStructVect();
};

I get this error:

invalid covariant return type for ‘virtual std::vector<ProvaSon::myStruct, std::allocator<ProvaSon::myStruct> > ProvaSon::get_myStructVect()’

Am I doing something wrong or maybe I'm trying to do something that is forbidden by the language?

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

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

发布评论

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

评论(5

脸赞 2024-11-07 20:07:30

这两个 myStruct 类型完全不相关。这意味着您正在尝试重写 TestFather::get_myStructVect() 并让它返回完全不同的类型。这是不允许的。

The two myStruct types are totally unrelated. This means that you're trying to override TestFather::get_myStructVect() and have it return a completely different type. This is not allowed.

野稚 2024-11-07 20:07:30

您不必在 TestSon 中重新定义结构体,而且您也不能这样做。 fatherptr->get_myStructVect 的调用者静态获取 vector 返回,因此编译器禁止您像这样重写基类函数,因为动态返回的对象可能与 vector 不兼容(谁知道你在 TestSon::myStruct 中放入了什么以及如何 vector行为与基类向量不同?)。

至于允许的差异,C++只允许派生类返回类型是基类返回类型的派生类,并且仅当返回类型是指针或引用时才允许。

You don't have to redefine the struct in TestSon, and you also can't do this. The caller of fatherptr->get_myStructVect statically gets a vector<TestFather::myStruct> back, so the compiler forbids you to override the base class function like that, because the dynamically returned object would potentially be incompatible with vector<TestFather::myStruct> (who knows what you put into TestSon::myStruct and how vector differs in behavior from the base class vector?).

As for the allowed difference, C++ only allows the derived class return type to be a derived class of the base class return type, and only when the return types are pointers or references.

一抹微笑 2024-11-07 20:07:30

这是禁止的。因为您在派生类中重新定义了 myStruct,所以 vector派生类中的类型与基类中的类型不同。它们是两个不同的 myStructs。

您只能将该虚拟函数的返回类型更改为从基类中声明的返回类型继承的类型。但是向量不继承自 vector

It's forbidden. Because you've redefined myStruct in your derived class, vector<myStruct> is a different type in the derived class than it is in the base class. They are two different myStructs.

You can only change the return type of that virtual function to something inherited from the return type declared in the base class. But vector<TestSon::myStruct> does not inherit from vector<TestFather::myStruct>

べ映画 2024-11-07 20:07:30

vector 但由于您在子类中更改了myStruct,它实际上是两种不同的类型,因此两个函数中的每一个都认为其返回不同的类型。这仅适用于协变类型,其中类型与包含该函数的类的实际类型相关。

请注意,无论如何,您可能不应该在此处按值返回类属性向量。

我不知道你真正打算做什么,但是如果嵌套结构是两个不同的东西,那么它们确实不应该具有相同的名称(如果它们相同,则不要重新定义它)。我的第一直觉反应是,也许亲子关系在这里不合适。您考虑过其他选择吗?如果您确实需要在子级中返回不同的类型并且父级不知道这一点,那么您不能使用虚拟接口来执行此操作。您应该给函数指定不同的名称,这样您就知道返回类型应该是什么。

通过有关您目标的更多详细信息,可以提供更好的答案。

It says vector<myStruct> but since you changed myStruct in the child class it's actually two distinct types so each of the two functions thinks its returning a different type. This is only allowed for covariant types where the type is related to the actual type of the class containing the function.

Note that you probably shouldn't be returning a class attribute vector by value here anyway.

I can't tell what you really are intending to do, but the nested structures really shouldn't have the same name if they're two different things (and if they're the same, don't redefine it). My first gut reaction is that maybe the parent-child relationship isn't appropriate here. Have you considered other options? If you really need to return a different type in the child and the parent doesn't know about that, then you can't use a virtual interface to do this. You should just give the functions different names so you know what the return type should be.

With more details about your goals a better answer could be provided.

冬天的雪花 2024-11-07 20:07:30

在您的示例中,重复的私有结构有点奇怪。例如,下面的代码可以正常编译:

class TestFather {

protected:
    struct myStruct
    {
        // struct definition
    };
    vector<myStruct> myStructVect;

public:
    virtual vector<myStruct> get_myStructVect() = 0;
};

class TestSon : public TestFather{



public:
    vector<myStruct> get_myStructVect();
};

int main(int argc, char**argv)
{
   TestSon testSon;
}

请注意将 private 替换为 protected,允许派生类访问父结构定义和 myStructVect 存储。

The duplicate private structure is a bit odd in your example. The below compiles fine, for instance:

class TestFather {

protected:
    struct myStruct
    {
        // struct definition
    };
    vector<myStruct> myStructVect;

public:
    virtual vector<myStruct> get_myStructVect() = 0;
};

class TestSon : public TestFather{



public:
    vector<myStruct> get_myStructVect();
};

int main(int argc, char**argv)
{
   TestSon testSon;
}

Note the replacement of private with protected, allowing derived classes access to the parent structure definition and myStructVect storage.

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