【发布时间】:2011-10-07 17:22:09
【问题描述】:
可能重复:
Operator overloading
编辑 2
我错误地使用了 insert(...),我实际上并不需要 '=' 运算符。很抱歉浪费人们的时间。我已经投票结束.. 还剩 2 票。请投票。
编辑
我想要一个 '=' 运算符的原因是我可以在 Derivation 对象的向量上使用 insert(...) 函数。目前我的编译器说:
/usr/include/c++/4.2.1/bits/stl_algobase.h:283: error: no match for 'operator=' in '* __result = * __first'
我之前为自己的类创建了 '==' 和 '
class Derivation {
public:
string rc;
ImplementationChoice Y;
vector<Derivation> X;
vector<string> D;
vector<string> C;
vector<Player> P, O;
vector<Attack> B;
// various functions
// ...
};
我想知道我需要放入什么
// What do '=' return? An object of the class right?
Derivation& operator=(const Derivation &d) const {
// something....
}
非常感谢。
【问题讨论】:
-
至少,它应该返回一个引用 (
Derivation&) 而不是对象的新副本。 :) -
谢谢您.. 现在编辑帖子。抱歉.. 对 C++ 很陌生
-
我建议你阅读operator overloading faq。
-
如果 ImplementationChoice 已经提供了一个
operator=,则很可能已经隐式声明了一个适当的Derivation::operator=。 (没有给出答案,因为它并没有真正解决你的问题,只是把它作为一个提醒。)
标签: c++