Solved

VC++ Error C2677 No Global Operator Found

Posted on 2002-03-21
8
1,085 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 78
PDF library for Delphi 2 104
C++ mouse_event mouse look 7 69
show out valin of json for debugging in visaul c++ 1 15
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 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

23 Experts available now in Live!

Get 1:1 Help Now