Solved

VC++ Error C2677 No Global Operator Found

Posted on 2002-03-21
8
1,113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6886939
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
ID: 6886959
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
ID: 6886980
>>> Error

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

>>> fix

     Test( CString("001") + T1 + "\r\n");
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 6886986
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
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6886998
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
ID: 6887039
Thanks very much, it works now (I guess we all learn something new every day ;)
0
 
LVL 86

Expert Comment

by:jkr
ID: 6887119
Thank you!

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

Expert Comment

by:jkr
ID: 6887482
>>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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

724 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