在这种情况下如何重载“+”运算符,

Posted

技术标签:

【中文标题】在这种情况下如何重载“+”运算符,【英文标题】:How to overload '+' operator for this case, 【发布时间】:2012-08-18 16:48:56 【问题描述】:

由于此错误,我正在尝试重载 + 运算符:

tools.cpp(147) : error C2679: binary '+' : no operator found which 采用“CSchemaString”类型的右手操作数(或者没有 可接受的转换)

代码是:

// I TRIED THIS TO OVERLOAD + OPERATOR
CSchemaString operator +(const CVisuComm& CVisuComm::TabCCUImess, 
                         const CSchemaString&       GetPriority);

//THE CODE AT WHICH THE ERROR IS RETURNED, 
//error C2679: binary '+' : no operator found which takes 
//a right-hand operand of type 
//'CSchemaString' (or there is no acceptable conversion)
CVisuComm::TabCCUImess[CVisuComm::nbCCUImess++]=
                        "RECONFIGURATION SOLUTION N° " +config.GetPriority();

//THE VARIOUS TYPES ARE
//CVisuComm-------class CVisuComm
//TabCCUImess-----static CString 
//nbCCUImess------static int nbCCUImess
//config----------CConfig_State_ChangeType 
//GetPriority-----CSchemaString

但它会与

一起引发相同的错误

错误 C2751: 'CVisuComm::TabCCUImess' : 函数名 参数不能限定

所以请提出正确的方法来重载运算符。任何帮助表示赞赏。

【问题讨论】:

请编辑您的问题以改进格式和组织。 【参考方案1】:

第二条错误信息给你一个线索:the name of a function parameter cannot be qualified

您的第一个参数名称应该只是一个名称。目前它是用类名限定的。

代替:const CVisuComm& CVisuComm::TabCCUImess

也许应该是:const CVisuComm& Comm

【讨论】:

以上是关于在这种情况下如何重载“+”运算符,的主要内容,如果未能解决你的问题,请参考以下文章