问题描述
我正在将其他人的C ++代码从AIX移植到linux,并试图进行必要的更改以使它能够构建(和工作),而不必深入研究代码的作用。 C ++不是我的强项;-)
无论如何,以下代码片段可以在AIX上编译并正常运行:
class Spot;
class PostTransfer {
public:
void removePosting(Spot &s);
}
//... in a PostTransfer() method there's this...
multiset <Spot,Compare_Spots> setRpst;
...
multiset <Spot,Compare_Spots>::iterator itr = setRpst.begin();
while(itr != setRpst.end()) {
this->removePosting(*itr);
}
但是g ++编译器抱怨将 const Spot 传递给需要 reference 到 non-const Spot 的函数。
PostTransfer.cpp:758:24: error: binding reference of type ‘Spot&’ to ‘const Spot’ discards qualifiers
758 | this->removePosting(*itr);
| ^~~~~~~~~
In file included from PostTransfer.cpp:62:
PostTransfer.hpp:89:28: note: initializing argument 1 of ‘void PostTransfer::removePosting(Spot&)’
89 | void removePosting(Spot &s);
当我将* itr转换为(Spot&)* itr时,编译器就可以了。
this->removePosting((Spot &) *itr);
但是将取消引用的迭代器强制转换为引用是否是洁食?这是否只是将迭代器作为参考传递并接受我告诉它忽略要迭代的多集的(const)性质?还是生成对临时Spot对象的新引用并将其传递?您认为在没有强制转换的情况下AIX xlC编译器在做什么?即* itr是引用还是对象?
顺便说一句,我要解决此问题的第一个尝试是将已取消引用的迭代器显式复制到本地Spot对象中并使用它。我很确定这行得通,但是多余的变量和代码行似乎很尴尬:
Spot s = *itr;
this->RemovePosting(s);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)