接收错误:“错误:静态断言失败:结果类型必须是根据输入范围的值类型构造的。构造班级向量时

发布于 2025-01-26 18:58:20 字数 1788 浏览 1 评论 0原文

我正在尝试创建一个脚本,该脚本使用多态性来创建父/子类结构中的一组链接向量。到目前为止,我已经设置了类,但是在尝试测试代码时,我会收到涉及静态断言的错误。在按行分析代码时,当我调用'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',line:

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

我一直在尝试让父母跟踪儿童课程,但我不熟悉媒介和指针,所以我有点卡住了。前进的任何帮助将不胜感激。

I am trying to create a script that uses polymorphism to create a set of linking vectors within parent/child class structure. So far I have got the classes setup, but when trying to test the code I receive an error involving static assertion. When analysing the code line by line, I run into the error when I call 'vector.begin() & vector.end()'.

Here is the full script:

#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()}};
}

Compiling gives this error:

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

I have narrowed down the cause of the error to this section:

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

Following the root of the problem leads me to the file 'stl_uninitialized.h', and the line:

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

I have been trying to get the child classes to be tracked by the parents but I am unfamiliar with vectors and pointers so I am a bit stuck. Any help with moving forward would be greatly appreciated.

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

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

发布评论

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

评论(2

如果继承不是公开的,则升级到基类是不明式的。即,

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

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

这正是最后一个作业表达式正在尝试做的事情。您将需要公开继承。即

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
{
};

Upcast to a base class is ill-formed if the inheritance is not public. i.e

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

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

That's exactly what that last assignment expression is trying to do. You will need to inherit publically. i.e

class A

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

class B : public A
{
};

class C : public A
{
};

class D : public A
{
};

EDIT:
This is the answer originally cited:

class A

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

struct B : A
{
};

struct C : A
{
};

struct D : A
{
};
定格我的天空 2025-02-02 18:58:20

我的情况与上述不同。我发现错误定义新类型的错误结果。具体来说,我在单个标头文件中定义了一种新类型:

typedef std::vector<vector<uint32_t>> RRset;

错误是vector&lt中声明的; &gt; 未通过std ::指定。该标头文件中没有报告错误,但是实际上未定义新类型,这会导致我的错误。

My case is different from the above. I find my error results from my misdefinition of a new type. Specifically, I defined a new type as follows in an individual header file:

typedef std::vector<vector<uint32_t>> RRset;

The mistake is that the vector declared inside < > is not specified by std::. No error is reported in that header file, but the new type is actually not defined, which causes my error.

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