【发布时间】:2020-09-11 07:34:24
【问题描述】:
我最近在做一个项目,偶然发现了以下情况
using namespace std;
//I have two structs, A basic struct as shown below
struct myAnotherStruct{
int x;
int y;
};
//A struct which embeds the stack of above struct type
struct myStruct{
int a;
float b;
stack <myAnotherStruct> s;
};
//Somewhere else in the code, I created this
stack <myStruct> t;
//And performed following
struct myAnotherStruct var;
var.x = 10;
var.y = 20;
// But, when I performed the following operation, there was a core Dump!
t.s.push(var)
现在,我的问题如下,
为什么是核心转储?每次添加新元素时不应该分配内存吗? var 应该被复制并且变量的存储类(在我的例子中是 var)应该无关紧要!
这样做是不是一个好方法?因为当我用谷歌搜索时,我总是得到“结构堆栈,结构向量”等,但不是其他方式(即堆栈结构)。
【问题讨论】:
-
请创建一个minimal reproducible example。
t.s.push(var)无法编译,因为stack <myStruct> t没有成员s。