解决使用重载运算符时的临时错误
Taking adress of temporary error while using overloaded operator
我的c ++项目中有一个字符串类。我必须使用双链表并创建自己的字符串类。在我的字符串类中,我必须重载<
、>
和==
运算符。实际上我做到了。但是在我的另一个类中,我有一个列表函数来比较我的字符串类。在这个比较中,我遇到了"临时地址"错误。
这是我的字符串类:
#include "String.h"
String::String(int coming)
{
x=coming;
}
int String::getX()
{
return x;
}
String String::operator==(String *taken)
{
return String (x==taken->x);
}
这是我的上市方法:
void myclass::list(String *taken)
{
otherclass *temp=head;
while(temp!=NULL)
{
if(&temp->get_string()==taken)//where i get error message.
cout<<temp<<endl;
temp=temp->get_nextnode();
}
}
编译器准确地告诉你问题:你不能获取临时对象的地址。 temp->get_string()
是一个临时对象1,并且您正在尝试获取其地址。
我不太确定你在这里的目的是什么,所以我不能建议修复。 但我强烈建议将String::operator==
定义为将指针定义为右侧会导致混淆。 让它再返回一次String
也毫无意义;通常,人们会期望==
的计算结果为布尔值。
1.嗯,大概。 您尚未显示
get_string()
声明.
相关文章:
- 在运算符重载定义中使用成员函数(const错误)
- 逻辑运算符上出现错误代码 a')'
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++:需要帮助了解运算符重载错误
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- C++分数混合比较运算符错误
- 为什么我收到错误:"运算符<<不匹配?
- 迭代器类的重载前缀增量运算符会引发分段错误
- 在 boost::qi 中使用过多的替代运算符会导致分段错误
- 调用运算符时出现链接器错误<<,如何解决?
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 解决错误:运算符 [] 不匹配
- 提升错误:"运算符=="的重载不明确
- 字符串输入中没有匹配的错误运算符错误
- 错误:运算符 = IN C++ 不匹配
- QT 创建器错误(运算符 + 不匹配)
- 错误C++运算符重载
- 错误:运算符==不匹配
- C++ 语法和编译器错误 - 运算符不匹配<<
- C++错误--运算符=不匹配