接收错误:“错误:静态断言失败:结果类型必须是根据输入范围的值类型构造的。构造班级向量时
我正在尝试创建一个脚本,该脚本使用多态性来创建父/子类结构中的一组链接向量。到目前为止,我已经设置了类,但是在尝试测试代码时,我会收到涉及静态断言的错误。在按行分析代码时,当我调用'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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果继承不是公开的,则升级到基类是不明式的。即,
这正是最后一个作业表达式正在尝试做的事情。您将需要公开继承。即
编辑:
这是最初引用的答案:
Upcast to a base class is ill-formed if the inheritance is not public. i.e
That's exactly what that last assignment expression is trying to do. You will need to inherit publically. i.e
EDIT:
This is the answer originally cited:
我的情况与上述不同。我发现错误定义新类型的错误结果。具体来说,我在单个标头文件中定义了一种新类型:
错误是
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:
The mistake is that the
vector
declared inside< >
is not specified bystd::
. No error is reported in that header file, but the new type is actually not defined, which causes my error.