在结构体中使用列表
Using a list inside a struct c++
我从编程开始,我的英语不好,很抱歉。
我喜欢在结构体内部使用列表而不是数组,就像这样:
#include <iostream>
#include <list>
using namespace std;
struct Market {
string b;
list <int> prices;
};
int main()
{ list <int> precios;
Market m1 = {"a",NULL};
return 0;
}
但是我得到这个错误转换从int' to non-scalar type std::list<int, std::allocator<int> >
请求|
这可能吗?也许用malloc或free?
应该定义一个构造函数
struct Market {
Market(string val){b=val;}
// or like this:
// Market(string val):b(val){}
string b;
list <int> prices;
};
然后你就可以创建这样的对象:
Market a("A");
由于list的默认构造函数创建的是空列表,所以你不需要给它传递任何参数。
关于类基础知识的好读物:http://www.cplusplus.com/doc/tutorial/classes/
NULL
不是std::list<int>
类型,这就是为什么你得到这个错误。
你正在使用c++ 11编译器吗?
如果是,则尝试:
Market m1 = {"a", { NULL } };
否则:
list<int> prices;
Market m1;
m1.b = "a";
m1.prices = prices;
您正在尝试用空指针值(实际上是int
类型)初始化list
。如果您需要按值存储列表,您可以初始化'm1',如下所示
Market m1 = {"a", std::list<int>()};
相关文章:
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 包含 std::list 的结构体的 C++ 初始化
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 如何使用结构体的向量数组?
- 如何使用结构体在C++中更改这些代码?
- 无法在 Mosquitto MQTT Broker 插件上访问结构体 mosquitto 的元素
- 使用 boost::p ython,如何将结构体向量作为字典列表返回给 Python
- 列表排序和结构体向量排序之间的性能差距.c++
- 在结构体中使用列表
- 如何根据结构体的字段之一对结构指针列表进行排序?
- 用大括号括起来的初始化列表初始化结构体时出错
- c++ std::指向结构体的指针列表
- 处理结构体列表的向量
- 如何使用迭代器比较结构体列表
- 使用迭代器从列表中打印结构体值
- c++初始化列表的元素比结构体少
- 二进制搜索在一个排序列表与所有节点的结构体c++
- 带结构体的列表映射(包含列表)——不能通过映射访问结构体中的列表