Click here to Skip to main content
14,698,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to build a legacy Win32 static lib project. it added its own CString and CFile class. they are intended to avoid direct use of MFC library, but kepts the same operator overloads. so when I compile this project, it throws error message:

Severity	Code	Description	Project	File	Line	Suppression State
Error	C2666	'CString::operator +': 2 overloads have similar conversions	diffengine	c:\demo_temp\diffengine\filepartition.cpp	443	


the error is pointed at the second plus sign +:
for (i=0; i<nbFiles; i++)
		AddString( szIndent + "    " + arrFiles.GetAt(i) );


so I think my win32 static library project is configured to use MFC somewhere, but I selected use windows standard libraries already in project property page.

could anyone tell me how to remove this declaration conflicts?

thanks a million!

What I have tried:

I build a new empty win32 static library project, then added these files from legacy project. these error messages are thrown.
Posted
Updated 29-Aug-20 12:44pm
Comments
Richard MacCutchan 29-Aug-20 5:11am
   
Please show the code for your CString class that has the overloads.
Southmountain 29-Aug-20 17:45pm
   
I copied overloaded operator here:
class CString
{
    // Members
protected:
    char *m_str;

    // Constructor/Destructor
public:
    CString() 
    { 
        m_str = NULL;
    }
    CString(char *szInputSource) 
    { 
        m_str = NULL;
        if (szInputSource)
            Copy(szInputSource);
    }
    CString(const CString &szInputSource) 
    { 
        m_str = NULL;
        Copy(szInputSource.GetData());
    }
    ~CString()
    {
        Empty();
    }

// Methods
public:
    void operator +=(CString &szAdditionalString)
    {
        Append(szAdditionalString);
    }

    void operator +=(char *szAdditionalString)
    {
        Append(szAdditionalString);
    }

    void operator +=(char sAdditionalChar)
    {
        Append(sAdditionalChar);
    }

    CString operator +(CString &szAdditionalString)
    {
        CString szTemp = *this;
        szTemp.Append(szAdditionalString);
        return szTemp;
    }

    CString operator +(char *szAdditionalString)
    {
        CString szTemp = *this;
        szTemp.Append(szAdditionalString);
        return szTemp;
    }

    CString operator +(char sAdditionalChar)
    {
        CString szTemp = *this;
        szTemp.Append(sAdditionalChar);
        return szTemp;
    }

// binary + operator (global, with const parameters)
    friend CString operator+(const CString &sz1, const CString &sz2)
    {
        CString s = sz1;
        s += (CString&)sz2;
        return s;
    }

    friend CString operator+(CString &sz1, const char sz2)
    {
        CString s = sz1;
        s += (char)sz2;
        return s;
    }

    friend CString operator+(const char *sz1, CString &sz2)
    {
        CString s = (char*)sz1;
        s += sz2;
        return s;
    }

1 solution

You can try instantiate CString for its + operator to work.

char* and char[] types does not have + operator

for (i=0; i<nbFiles; i++)
		AddString( szIndent + CString("    ") + arrFiles.GetAt(i) );
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900