Solved

VC++ Error C2677 No Global Operator Found

Posted on 2002-03-21
8
1,092 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

813 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

16 Experts available now in Live!

Get 1:1 Help Now