编译不了?提示 overloads have similar conversions

zhucaiguai 2007-09-22 09:22:07
我写了两个函数,如下:
BOOL operator == (const myString& str) const
{
return str == pszString;
}

BOOL operator != (const myString& str) const
{
return str != pszString;
}

编译通不过,提示如下:
error C2666: 'myString::operator !=' : 3 overloads have similar conversions


求助啦~
...全文
1382 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
vocanicy 2007-09-22
  • 打赏
  • 举报
回复
楼主你的类应该没有问题,VC6下一切正常,估计和编译器对有关
MSDN中也提到不同编译器对这种情况处理不同
2005也许加强了这方面的检查


我估计是这个原因
BOOL operator != (LPCTSTR pszStr) const
BOOL operator != (const fsString& str) const

由于你写了构造函数fsString(LPCTSTR)
当与比较 LPTSTR类型进行比较的时候,编译器无知道该调用operator != (LPCTSTR pszStr)还是调用BOOL operator != (const fsString& str) const好
因为LPTSTR可以转换成LPCTSTR,也可以转换成const fsString

由于你重载了operator LPCTSTR()
如果和fsString类型比较时,编译器无知道该调用operator != (LPCTSTR pszStr)还是调用BOOL operator != (const fsString& str) const好
因为fsString可以转换成LPCTSTR,也可以转换成const fsString


建议你试试将其中一个函数去掉试试
shanhqk 2007-09-22
  • 打赏
  • 举报
回复
BOOL operator == (const myString& str) const
{
return str == pszString;
}

BOOL operator != (const myString& str) const
{
return str != pszString;
}
我认为==和!=不能这样定义。
因为LZ需要使用==和!=,但是LZ明显的是为myString类定义这两种操作符的。
LZ必须为其中的成员变量来写==和!=,而不能直接使用这两个。

BOOL operator == (const fsString& str) const
{
return this.pszString == str.pszString;
}

BOOL operator != (const fsString& str) const
{
return str.pszString != this.pszString;
}


最好还是使用:_tcscpy相同的比较函数好一点,具体的我不是很清楚,LZ可以查一下。
shanhqk 2007-09-22
  • 打赏
  • 举报
回复
BOOL operator == (const myString& str) const
{
return str == pszString;
}

BOOL operator != (const myString& str) const
{
return str != pszString;
}
我认为==和!=不能这样定义。
因为LZ需要使用==和!=,但是LZ明显的是为myString类定义这两种操作符的。
LZ必须为其中的成员变量来写==和!=,而不能直接使用这两个。

BOOL operator == (const fsString& str) const
{
return this.pszString == str.pszString;
}

BOOL operator != (const fsString& str) const
{
return str.pszString != this.pszString;
}


最好还是使用:_tcscpy相同的比较函数好一点,具体的我不是很清楚,LZ可以查一下。

shanhqk 2007-09-22
  • 打赏
  • 举报
回复
最好还是使用:_tcscpy相同的比较函数好一点,具体的我不是很清楚,LZ可以查一下。
zhucaiguai 2007-09-22
  • 打赏
  • 举报
回复
编译通过,多谢楼上的兄弟~

结贴,给分,睡觉^_^
ckt 2007-09-22
  • 打赏
  • 举报
回复
to:shanhqk(山)

return this.pszString == str.pszString;

字符串比较应该是比较内容,不是首地址吧
ckt 2007-09-22
  • 打赏
  • 举报
回复
stccmp(pszString, str.pszString)
比较两个字符串是否相等

你这个结构其他有的地方写的很乱
可以去网上在找点资料参考
shanhqk 2007-09-22
  • 打赏
  • 举报
回复
BOOL operator == (const fsString& str) const
{
return this.pszString == str.pszString;
}

BOOL operator != (const fsString& str) const
{
return str.pszString != this.pszString;
}
ckt 2007-09-22
  • 打赏
  • 举报
回复
BOOL operator == (const fsString& str) const
{
return str == pszString;
}

BOOL operator != (const fsString& str) const
{
return str != pszString;
}

// 就是因为不能直接比较,所以你才要重载== 和 !=
// 所以你不能直接用str == pszString;和str != pszString;
  • 打赏
  • 举报
回复
BOOL operator == (const myString& str) const
{
return str.pszString == pszString;
}

BOOL operator != (const myString& str) const
{
return str.pszString != pszString;
}

这样才对.
zhucaiguai 2007-09-22
  • 打赏
  • 举报
回复
/*
Free Download Manager Copyright (c) 2003-2007 FreeDownloadManager.ORG
*/



#ifndef __FXS_STRING_H_
#define __FXS_STRING_H_

#include <tchar.h>
#include <stdio.h>

struct fsString
{
LPTSTR pszString;

fsString ()
{
pszString = NULL;
}

~fsString ()
{
if (pszString)
delete [] pszString;
}

fsString (const fsString& str)
{
pszString = NULL;
*this = str.pszString;
}

fsString (LPCTSTR str)
{
pszString = NULL;
*this = str;
}

LPCTSTR operator = (LPCTSTR pszStr)
{
if (pszString)
{
delete [] pszString;
pszString = NULL;
}

if (pszStr)
{
pszString = new TCHAR [_tcslen (pszStr) + 1];
if (pszString)
_tcscpy ( pszString, pszStr );
}

return pszString;
}

fsString& operator = (const fsString& str)
{
*this = str.pszString;
return *this;
}

LPCTSTR operator += (LPCTSTR pszStr)
{
if (pszStr == NULL)
return pszString;

if ( pszString )
{
LPTSTR pszOld = pszString;

pszString = new TCHAR [ _tcslen (pszString) + _tcslen (pszStr) + 1 ];
_tcscpy ( pszString, pszOld );
_tcscat ( pszString, pszStr );

delete [] pszOld;
}
else
{
*this = pszStr;
}

return pszString;
}

LPCTSTR operator += (char c)
{
char sz [2];
sz [0] = c; sz [1] = 0;
return *this += sz;
}

fsString operator + (LPCTSTR psz) const
{
fsString str = *this;
str += psz;
return str;
}

BOOL operator == (const fsString& str) const
{
return str == pszString;
}

BOOL operator != (const fsString& str) const
{
return str != pszString;
}

BOOL operator == (LPCTSTR pszStr) const
{
if (pszString == NULL || pszStr == NULL)
return pszStr == pszString;

return _tcscmp ( pszString, pszStr ) == 0;
}

void clear ()
{
if (pszString)
{
delete [] pszString;
pszString = NULL;
}
}

void ncpy (LPCSTR pszStr, int nch)
{
alloc (nch);
strncpy (pszString, pszStr, nch);
}

void alloc (int nch)
{
clear ();
pszString = new char [nch+1];
pszString [nch] = 0;
}

BOOL operator != (LPCSTR pszStr) const
{
return !(*this == pszStr);
}

operator LPTSTR () const
{
return pszString;
}

int Length ()
{
if (pszString)
return _tcslen (pszString);
else
return 0;
}

BOOL IsEmpty ()
{
return pszString == NULL || *pszString == 0;
}

int GetLength () {return Length ();}

void Replace (LPCSTR , LPCSTR ) {}

void Format (LPCSTR pszFormat ...)
{
LPSTR psz = new char [100000];
va_list ap;
va_start (ap, pszFormat);
vsprintf (psz, pszFormat, ap);
va_end (ap);
*this = psz;
delete [] psz;
}
};

#endif

完整代码如上~


shanhqk 2007-09-22
  • 打赏
  • 举报
回复
BOOL operator == (const myString& str) const
{
return str == pszString;
}

BOOL operator != (const myString& str) const
{
return str != pszString;
}
我认为==和!=不能这样定义。
因为LZ需要使用==和!=,但是LZ明显的是为myString类定义这两种操作符的。
LZ必须为其中的成员变量来写==和!=,而不能直接使用这两个。
真相重于对错 2007-09-22
  • 打赏
  • 举报
回复
return str != pszString;
pszString 是什么类型??

  • 打赏
  • 举报
回复
我用vs2005编了,也没有问题,还是把你完整的类贴出来吧..
真相重于对错 2007-09-22
  • 打赏
  • 举报
回复
类定义
???
真相重于对错 2007-09-22
  • 打赏
  • 举报
回复
形参表过于类似,无法解析多义性。
ckt 2007-09-22
  • 打赏
  • 举报
回复
BOOL operator == (const myString& str) const
{
return str == pszString;
}

BOOL operator != (const myString& str) const
{
return str != pszString;
}


你myString类有重载!= 和 == 么
zhucaiguai 2007-09-22
  • 打赏
  • 举报
回复
我用的是VC2005,你用的是什么啊?
  • 打赏
  • 举报
回复
#include <string>

class T
{
public:
explicit T() {}
explicit T( const T & t ) { printf("In T(t)\n"); }
T &operator=( const T &t ) { printf("In =(t)\n");}
bool operator==(const T& t) { return t.value == value; }
bool operator!=(const T& t) { return t.value != value; }

private:
std::string value;
};

int main()
{
T a;
T b;
if (a == b)
;
return 0;
}

我这样写没有问题啊?是不是你的编绎器不支持?

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧