两个类使用彼此的成员类型

我定义了A类和B类,如下所示:

class A
{
public:
    using Type = int;
    B::Type v;
};

class B
{
public:
    using Type = double;
    A::Type v;
};

然后我得到一个编译错误

 error: 'B' does not name a type

以下代码也不起作用。

class B;

class A
{
public:
    friend class B; // makes no sense
    using Type = int;
    B::Type v;
};

class B
{
public:
    using Type = double;
    A::Type v;
};

您知道,一个类不能定义两次,因此A必须在B之后定义,或者B必须在A之后定义。

如何使用彼此的成员类型?

方法可以在外部定义,但是成员类型可以在类外部定义吗?

我唯一的解决方案是将类型移出类。

weiqingtao 回答:两个类使用彼此的成员类型

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3147511.html

大家都在问