请问我在一个类中重载了‘=’,如果没有用到它 的时候能通过编译,一使用到它就出现错误。error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class OperationData' (or there is no acceptable conversion)我是这样定义的:
void operator=(const OperationData od);请问是什么原应啊?
void operator=(const OperationData od);请问是什么原应啊?
OperationData& operator=(const OperationData od);
还是出现同样的错误。我该成这样:
OperationData& operator=(const OperationData &od);
出现error C2511: '=' : overloaded member function 'class OperationData &(class OperationData &)' not found in 'OperationData'
{
while(this->holes.length()>0)
this->holes.removeFirst();
while(this->barsIn.length()>0)
this->barsIn.removeFirst(); this->pos = old.pos;
copyPolygon(this->contour,old.contour);
this->holes = old.holes;
this->barsIn = old.barsIn;
return *this;
}
在类中定义的话,这是个双目运算符,参数为右操作数.使用时需要提供左操作数:
OperationDatao a ;
OperationData b = a ;//等价于b.operator = (a);
重载=一般用于将其它类对象转化成本类对象,而a,b同类,标准做法是使用拷贝构造函数.
而且不知编译器将会对同类对象的operation=做何感想.
拷贝构造函数的定义:
class OperationData {
...
OperationData(const OperationData& od ) ;
...
} ;
只要有拷贝构造函数,不必重载=,就可以直接用=进行上面的b=a的操作.全局范围内定义时,是个单目运算符,这样的话改变了=的原始语义,可能思路有些问题.
///////fstring.h
struct fstring
{
#define DEFAULT_STR_LEN 5024
public:
fstring();
fstring(const char* src);
fstring(fstring& src);
~fstring();
bool CopyFromBuffer(const char* pBuf, const unsigned int& nSize);
bool TrimLeft();
bool TrimRight();
fstring& Trim();
bool EmptyBuffer();
bool ReadStrFromList(char* dst);
fstring SpiltStr(const char* spitstr);
fstring ReadStrFromList();
fstring Left(int len);
fstring& Reverse();
int Find(const char* src);
bool strcmp(const fstring& s);
int ReverseFind(const char* src);
fstring Right(int len);
fstring& Format(const char* lpszText,...);
int length();
fstring& operator=(const char* src);
fstring& operator=(const fstring& src);
fstring& operator+=(const char* src);
operator const char*( )const;
friend bool operator != (const char*& src, const fstring& fs);
friend bool operator == (const char*& src, const fstring& fs);
friend fstring Format(const char* lpszText, ...);
private:
fstring& Format(char* lpszText, va_list& list);
char* str;
unsigned int m_len;
};
////////fstring.cpp fstring& fstring::operator = (const char* src)
{
m_len = _tcslen(src)+1;
if (str)
{
cffree(str);
str = NULL;
};
str = (char*)cfmalloc(m_len);
memset(str, 0, m_len);
_tcscpy(str, src);
return *this;
} fstring& fstring::operator = (const fstring& src)
{
m_len = _tcslen(src)+1;
if (str)
{
cffree(str);
str = NULL;
};
str = (char*)cfmalloc(m_len);
memset(str, 0, m_len);
_tcscpy(str, src);
return *this;
}