重载一个simple =运算符 [英] overloading a simple = operator
问题描述
我试图重载我的类的一部分到字符串
,我不能得到重载工作。或者,我也将有一个 long long
重载,但我只是假设它将是相同的除了 long long
而不是 string
。
I am trying to overload part of my class to a string
and I can't get the overloading to work. Alternatively, I will also have a long long
overload, but I just assume that it will be the same excepted for long long
instead of string
.
class FileData
{
public:
string extensions_;
unsigned long long containsBytes_;
};
string& operator = (string& s , FileData& fd)
{
s= fd.extensions_;
return s;
}
string extName = fileVector[0];
我得到的错误是ERROR:'operator ='必须是一个成员函数。
我也试过使用istream,但也没有工作。
The error I keep getting is ERROR:'operator=' must be a member function. I also tried using istream but that also didn't work.
任何人都知道我该怎么做?
Anybody know how I could go about this?
推荐答案
您需要一个转换运算子:
class FileData
{
public:
// Conversion to string
operator string() const {return extensions_;}
string extensions_;
unsigned long long containsBytes_;
};
您只能将赋值运算符重载为类的成员, >到该类的对象。
You can only overload the assignment operator as a member of your class, for the purpose of assigning to an object of that class.
这篇关于重载一个simple =运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!