Solved

VC++ Error C2677 No Global Operator Found

Posted on 2002-03-21
8
1,102 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
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 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