Receiving error: "error: static assertion failed: result type must be constructible from value type of input range" when constructing class vectors

Receiving error: "error: static assertion failed: result type must be constructible from value type of input range" when constructing class vectors

我正在尝试创建一个脚本,该脚本使用多态性在 parent/child class 结构中创建一组链接向量。到目前为止,我已经设置了 classes,但是在尝试测试代码时,我收到了一个涉及静态断言的错误。当逐行分析代码时,我 运行 在调用 'vector.begin() & vector.end()'.

时进入错误

这是完整的脚本:

#include <iostream>
#include <vector>
using namespace std;

class A
{
public:
    vector<vector<A *>> Container;
};

class B : A
{
};

class C : A
{
};

class D : A
{
};

int main()
{
    A ABaseClass;

    B b1, b2;
    C c1, c2;
    D d1, d2;

    vector<B *> b_classes = {&b1, &b2};
    vector<C *> c_classes = {&c1, &c2};
    vector<D *> d_classes = {&d1, &d2};

    ABaseClass.Container = {
        {b_classes.begin(), b_classes.end()},
        {c_classes.begin(), c_classes.end()},
        {d_classes.begin(), d_classes.end()}};
}

编译报错:

error: static assertion failed: result type must be constructible from value type of input range
  138 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |                                                                        ^~~~~

error: static assertion failed: result type must be constructible from value type of input range
note: 'std::integral_constant<bool, false>::value' evaluates to false

我已经将错误原因缩小到这部分:

ABaseClass.Container = {
        {b_classes.begin(), b_classes.end()},
        {c_classes.begin(), c_classes.end()},
        {d_classes.begin(), d_classes.end()}};

根据问题的根源,我找到了文件 'stl_uninitialized.h' 和行:

[138]      static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
                                                                            ^~~~~

我一直在尝试让 child classes 被 parents 跟踪,但我不熟悉矢量和指针,所以我有点卡住了。任何对前进的帮助将不胜感激。

如果继承不是 public,则向上转换到基 class 是 ill-formed。即

struct base {};
class derived : base {}; // private inheritance

int main(){
    derived a;
    auto & b = static_cast<base &>(a); // invalid
}

这正是最后一个赋值表达式试图做的事情。您将需要继承 public盟友。即

class A

public:
vector<vector<A *>> Container;
};

class B : public A
{
};

class C : public A
{
};

class D : public A
{
};

编辑: 这是原引用的答案:

class A

public:
vector<vector<A *>> Container;
};

struct B : A
{
};

struct C : A
{
};

struct D : A
{
};