c++ - 取消引用指针列表的迭代器
问题描述
我在尝试取消引用 a 的迭代器时遇到问题std::list<std::unique_ptr<MyClass>>
。这是我的情况:在 headerFile.h 我有
class MyClass{
public:
bool variable = false;
private:
};
然后在 headerFile2.h
#include "headerFile.h"
#include <memory>
#include <list>
class OtherClass{
public:
private:
std::list<std::unique_ptr<MyClass>> MyList;
void MyFuction();
};
最后,在 headerFile2.cpp 中,我尝试像这样使用 MyClass::variable:
#include "headerFile2.h"
void OtherClass::MyFunction(){
for(auto it = MyList.begin(); it != MyList.end(); it++){
*it -> variable = true;
}
}
它不会编译,我不知道我的错误在哪里。错误信息是
'struct std::_List_iterator<std::unique_ptr<MyClass> >' has no member named 'variable'
我也试过做**it.variable = true;
。
我会很感激任何建议。
解决方案
operator->
的优先级高于operator*
,因此*it -> variable = true;
被解释为*(it -> variable) = true;
,whileit -> variable
无效。
您可以将括号添加为
(*it) -> variable = true;
**it.variable = true;
有类似的问题;你可以(**it).variable = true;
。
推荐阅读
- asp.net - 如何使用 enableVersionHeader 关闭我的网站 IIS 7 的服务器签名
- javascript - Firebase 将数据库中的数据设置为变量
- jenkins - 每次我重新启动服务器时,詹金斯大师都会把我锁在外面
- ionic2 - 如何在离子2中收取条带付款
- intellij-idea - Ballerinalang 配置
- android - android studio更新后构建错误
- c++ - (QT) 使用 Qt Creator 包含来自 Linux for Windows 应用程序的库
- php - 如何在 Wordpress 中的 URL 末尾添加“/”
- android - 未在片段中显示 tablayout 的内容
- php - 在 Woocommerce 3 中更新并显示更新的产品价格