overloaded operators

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Hey guys, quick question, I am trying to overload the = operator as in operator=() but I am getting errors when I try to do it globally. Doing it privately yields no errors at all.

For simplicity i have included only the header file but the definitions can be supplied if needed.

Rich (BB code):
#ifndef PRACTICE_H
#define    PRACTICE_H

#include <iostream>
#include <string>
using namespace std;

class practice
{
public:
    practice(const char[] = "empty");
    practice (const practice &);
    ~practice();

    char* reader();
    void input(const char*);
    void input(const string &);
    
    bool operator==(const practice &);
    bool operator!=(const practice &);
    practice & operator=(const practice &);
    
    friend practice & operator+(practice &, const practice &);
    friend istream & operator>>(istream &, practice &);
    friend ostream & operator<<(ostream &, const practice &);
    
private:
    char *ptr;
};

#endif
This method works just fine.

Rich (BB code):
#ifndef PRACTICE_H
#define    PRACTICE_H

#include <iostream>
#include <string>
using namespace std;

class practice
{
public:
    practice(const char[] = "empty");
    practice (const practice &);
    ~practice();

    char* reader();
    void input(const char*);
    void input(const string &);
    
    bool operator==(const practice &);
    bool operator!=(const practice &);
    
    friend practice & operator=(practice &, const practice &);
    friend practice & operator+(practice &, const practice &);
    friend istream & operator>>(istream &, practice &);
    friend ostream & operator<<(ostream &, const practice &);
    
private:
    char *ptr;
};

#endif
THis method yields the following errors:

1>------ Build started: Project: Exam practice, Configuration: Debug Win32 ------
1>Compiling...
1>Practice implementation.cpp
1>z:\college\year2\semester 2\programming\exam practice\exam practice\practice.h(22) : error C2801: 'operator =' must be a non-static member
1>z:\college\year2\semester 2\programming\exam practice\exam practice\practice implementation.cpp(58) : error C2801: 'operator =' must be a non-static member
1>z:\college\year2\semester 2\programming\exam practice\exam practice\practice implementation.cpp(59) : error C2248: 'practice::ptr' : cannot access private member declared in class 'practice'
1> z:\college\year2\semester 2\programming\exam practice\exam practice\practice.h(28) : see declaration of 'practice::ptr'
1> z:\college\year2\semester 2\programming\exam practice\exam practice\practice.h(9) : see declaration of 'practice'
1>z:\college\year2\semester 2\programming\exam practice\exam practice\practice implementation.cpp(59) : error C2248: 'practice::ptr' : cannot access private member declared in class 'practice'
1> z:\college\year2\semester 2\programming\exam practice\exam practice\practice.h(28) : see declaration of 'practice::ptr'
1> z:\college\year2\semester 2\programming\exam practice\exam practice\practice.h(9) : see declaration of 'practice'
1>Test file.cpp
1>z:\college\year2\semester 2\programming\exam practice\exam practice\practice.h(22) : error C2801: 'operator =' must be a non-static member
1>Generating Code...
1>Build log was saved at "file://z:\College\Year2\Semester 2\Programming\Exam practice\Exam practice\Debug\BuildLog.htm"
1>Exam practice - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
Last edited:

Mark44

Joined Nov 26, 2007
628
In your two versions of practice.h, I'm assuming the first one works fine, and the second one doesn't. It was a little hard to figure out what "this" referred to in each -- better would be to describe with "above" or "below."

The difference between the two examples seems to be that the first (and working) has an explicit declaration for an operator= operator and the second (nonworking) makes it a friend operator. I'm very rusty on these C++ intricacies and don't have time right now to research it, so will instead just try to point you in the appropriate directions.

There could be some other stuff going on behind the scenes in your second example. I believe that if you don't provide an operator=, you get one for free, so it could be there is some sort of confusion on the part of the compiler, which generates a default operator= and tries to generate the friend one you're declaring. It's not clear to me why you have specified friend for this operator in your second example. The basic idea, IIFC, is that you can confer friendship on another class. In two of your other operators, there is a connection to either istream or ostream, but that's not the case with your friend operator=. I'm really fuzzy about this, but I would suggest you read up on the friend concept as it applies to C++ classes.

Also take a look to see what you can find about the default methods and operators a compiler builds for a class. These include a default (no args) constructor, a default copy construction, and I think, operator=.

Hope this is helpful.
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Your input is always helpful Mark. I will try to see if I can find some more info about it. I will also ask my lecturer but I wont hold thumbs. I have a strange feeling that this operator cannot be overloaded globally (using it as a friend function)

I tried googling it to see if any others have the same problem.

Will let you know if I find anything
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Well i just found this on a website:

An operator function can be either a "friend" function or a "member" function of a class. A friend function has access to all private and protected members of a class for which it is a friend. A friend function is declared using a prototype within the class, preceded by the [COLOR=blue ! important][COLOR=blue ! important]keyword[/COLOR][/COLOR] friend. The following operators cannot be overloaded using friend functions: = () [] -

The link to the website that I found this on is:
http://www.gamespp.com/c/introductionToCppMetrowerksLesson09.html


Its a bit strange as to why they cant be overloaded but not to worry Mark, I was mainly just wandering if it was some obscure syntax that I was overlooking.

Thanks
 

Mark44

Joined Nov 26, 2007
628
I'm guessing that it was considered too insecure to offer access to everything nonpublic to a nonmember operator. Bjarne Stroustrup probably has something to say about it in The C++ Programming Language, Second Ed. As much coding in C++ as you do, this would be a good resource for you. Possibly it's out in a newer edition.
Mark
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
Indeed, you are right, he says:
11.2.2 Predefined Meanings for Operators [over.predefined]
Only a few assumptions are made about the meaning of a userdefined
operator. In particular,
o p e r a t o r =, o p e r a t o r [], o p e r a t o r (), and o p e r a t o r >
must be nonstatic member functions; this
ensures that their first operands will be lvalues (§4.9.6).
The meanings of some builtin
operators are defined to be equivalent to some combination of
other operators on the same arguments. For example, if a is an int, ++a means a +=1 , which in turn
means a =a +1 . Such relations do not hold for userdefined
operators unless the user happens to
define them that way. For example, a compiler will not generate a definition of Z :: o p e r a t o r +=()
from the definitions of Z :: o p e r a t o r +() and Z :: o p e r a t o r =().
Because of historical accident, the operators = (assignment), & (addressof),
and , (sequencing;
§6.2.2) have predefined meanings when applied to class objects. These predefined meanings can
be made inaccessible to general users by making them private:
c l a s s X {
p r i v a t e :
v o i d o p e r a t o r =(c o n s t X &);
v o i d o p e r a t o r &();
v o i d o p e r a t o r ,(c o n s t X &);
/ / ...
};
The
The book which i am using is c++ how to program 5th edition by deitel and deitel which I find to be very good, although in fairness I have no comparison as its the only book I have read.
If you reckon the above is good I will definitely give it a good attempt.
 

Mark44

Joined Nov 26, 2007
628
Stroustrup designed the language, so what more can I say? I had the Deitel&Deitel book in some edition at one time, and as I recall, it was pretty good. You can probably pick up a used copy of the Stroustrup one at abebooks.com.
 
Top