访问包含列表的对象列表的内容
Accessing the contents of a list of objects that contain a list
我已经试着让它工作了几个星期了。我很尴尬。我觉得答案可能简单得可笑。
我想定义一种包含列表的对象类型(即类(,例如类ListA{string category;list<string>components;…}。然后我想创建这些对象的列表,例如list<ListA>myMenu。正如你已经猜到的,我不能让它工作。
这是代码:
#include <iostream>
#include <list>
using namespace std;
class ListA {
public:
string category;
list<string> items;
ListA() { category = "Unentitled"; }
ListA(const ListA& orig){}
~ListA(){}
};
int main(int argc, char** argv) {
cout<<"nnTEST a6.3: Let's try an example that is more complete. ";
//setup lists for each entree
ListA ingredientsA;
ingredientsA.category="Pizza";
ingredientsA.items.push_back("tomatos"); ingredientsA.items.push_back("garlic");
ingredientsA.items.push_back("cheese"); ingredientsA.items.push_back("dough");
ListA ingredientsB;
ingredientsB.category="Milk shake";
ingredientsB.items.push_back("milk"); ingredientsB.items.push_back("ice cream");
ingredientsB.items.push_back("cocoa"); ingredientsB.items.push_back("whip cream");
//verify contents of lists are correctly added
cout<<"nTEST a.6.3.2[Category("<<ingredientsB.category<<")]";
cout<<"nTEST a.6.3.4[Ingredient("<<ingredientsB.items.front()<<")]";
//add to our list of entries
list< ListA > dishes;
dishes.push_back(ingredientsA);
dishes.push_back(ingredientsB);
//grab first entree in dishes list just added, what the heck?
ListA tmpa = dishes.front();
cout<<"nTEST a.6.3.5[Category("<<tmpa.category<<")]";
cout<<"nTEST a.6.3.6[Ingred size("<<tmpa.items.size()<<")]n";
//same with iterator
list< ListA >::iterator itra = dishes.begin();
cout<<"nTEST a.6.3.5[Category("<<itra->category<<")]";
cout<<"nTEST a.6.3.6[Ingred size("<<itra->items.size()<<")]n";
return 0;
}
输出:
TEST a6.3: Let's try an example that is more complete.
TEST a.6.3.2[Category(Milk shake)]
TEST a.6.3.4[Ingredient(milk)]
TEST a.6.3.5[Category()]
TEST a.6.3.6[Ingred size(0)]
TEST a.6.3.5[Category()]
TEST a.6.3.6[Ingred size(0)]
RUN FINISHED; exit value 0; real time: 10ms; user: 0ms; system: 0ms
我创建列表<ListA>,将ListA对象添加到其中,它们会立即尝试访问该列表的元素,但无法访问任何该死的东西。(参见a.6.3.5和.6;第一个是直接访问,第二个是使用迭代器。(
建议?我做错了什么傻事?
ListA(const ListA& orig){}
您的类有一个显式声明的复制构造函数,该构造函数不复制任何内容。
当您声明复制构造函数时,您将负责完全基于所复制对象的内容来构造新对象。
你的构造函数什么都不做。你最终构建了一个空对象。
dishes.push_back(ingredientsA);
所以,你创建了一个列表。然后通过push_back()
将ingredientsA
复制到列表的末尾。
由于您的复制构造函数并没有真正复制任何内容,因此您最终会将一个完全空的类实例复制到dishes
列表中。
只需去掉显式复制构造函数。它没有明显的目的。
//add to our list of entries
list< ListA > dishes;
dishes.push_back(ingredientsA);
dishes.push_back(ingredientsB);
使用复制构造函数将List放入std::List中,并且您的复制构造函数覆盖默认构造函数,并且不执行
相关文章:
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 从封装在对象中的函数 C++ 返回时为空的列表
- 如何在不销毁对象的情况下返回对象列表
- 如何使用Q_PROPERTY公开自定义对象列表
- 链接列表在 cpp 中包含不同的对象类
- 如何使用对象制作分数列表并获得平均值
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 我可以从列表中获取对象并复制它们,但如何删除我复制的对象?
- 如何获取列表的每个对象并调用getName方法来打印其名称
- 如何在Windows Media Foundation SDK中指定语言列表对象
- 在 DLL 中嵌入 Python:Py_DECREF列表对象时访问冲突读取位置
- 将类的每个实例添加到列表对象的构造函数
- 列表<对象*>迭代器中的分段错误
- 如何使用list::sort()对STL列表对象进行排序,同时将自定义排序函数作为参数传递给list::排序
- 如何防止客户端修改列表对象
- c++列表对象错误
- 在列表对象中使用对象的派生函数
- 编译器错误 使用列表<对象*的迭代器> C++