Solved

VC++ Error C2677 No Global Operator Found

Posted on 2002-03-21
8
1,076 Views
Last Modified: 2012-06-27
In attempting to compile the following code (available at
http://www.phibian.com/EEQ.zip), I receive the following error message:

Error C2677: Binary '+': no global operator found which takes type 'PString' (or there is no acceptable conversion)

I'm looking to find out why I get the error, and how to fix it :)  Unfortunately, I can't modify anything except the PString class, so (for example)
Test(PString("001") + T1 + PString("002"));
wouldn't help me.

For reference, I'm using the following development platform:
Visual C++ 7
Windows XP Pro

Thanks!
0
Comment
Question by:Phibian
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
The error message is pretty on point - you are missing both

    PString operator + (const PString &InString, const char* psz);
    PString operator + (const char *InString, const PString& pstr);

Either make these operators members of your class or define two global 'operator+()' and make them a 'friend' of PString...
0
 
LVL 1

Author Comment

by:Phibian
Comment Utility
Maybe I am missing something obvious here but if I do that I get:

error C2804: binary 'operator +' has too many parameters
0
 
LVL 6

Expert Comment

by:thienpnguyen
Comment Utility
>>> Error

     Test( "001" + T1 + "\r\n");

>>> fix

     Test( CString("001") + T1 + "\r\n");
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
Comment Utility
Ooops, that's why you need global operators, as the compiler cannot distinguish which class '+' belongs to - IOW, do NOT make them members of you class - use


class PString; // foward decl.
PString operator+(const PString&, const char*);
PString operator+(const char*, const PString&);

class PString {

friend PString operator+(const PString&, const char*);
friend PString operator + (const char*, const PString&);

//...
};

// implementation
PString operator+(const PString&, const char*) {

//...
}

PString operator+(const char*, const PString&) {

//...
}
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:thienpnguyen
Comment Utility
Sorry, I don't read your question carefully .

The jkr's comment works , for example

#include <fcntl.h>
#include <iostream.h>
#include <stdlib.h>

#include "PString.h"

void Test(CString Input)
     {
     cout << Input << endl;
     }

// new code

PString operator + (const char *InString, const PString& pstr)
{
    return PString( (char *) InString) + pstr;
}

main()
{
   // same as your code
}
0
 
LVL 1

Author Comment

by:Phibian
Comment Utility
Thanks very much, it works now (I guess we all learn something new every day ;)
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Thank you!

And thank you also for clarifying, thienpnguyen!
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>I guess we all learn something new every day ;)

I remember a proverb mentioning that if you stop to learn, it is a sure sign that you're dead...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now