如何打印列表<类*> LS?
how to print a list<class*> ls?
我在下面代码的最后两行做错了什么?我收到一个错误:
对
‘t.std::_List_iterator<_Tp>::operator* [with _Tp = a*]()’
中的成员‘name’
的请求,其为非类类型‘a*’
#include <dlfcn.h>
#include <iostream>
#include "/home/developer/Desktop/MsgSphSDK1/test1_sdk/libsdk_MS.hpp"
#include <list>
#include <typeinfo>
#include <string>
using namespace std;
class a
{
public:
string name;
int age;
};
int main()
{
a* l = new a();
l->name = "me";
l->age = 1;
list<a*> ls;
list<a*>::iterator t;
for (t = ls.begin(); t != ls.end(); ++t)
cout << (*t).name << endl;
}
您应该编写
cout<<(*t)->name<<endl
t
是一个迭代器,(*t)
为您提供a*
(指向类a
的对象的指针)。因此,要访问对象的成员,应该使用->
,而不是.
。
相关文章:
- EASTL矢量<向量<int>>连续的
- 执行ls命令并打印输出
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 具有读写功能的 UNIX 管道系统可执行"ls -la"命令
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- Intotify观察者在LS或观看命令后停止工作