Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C++

Problem with global operator functions in VC++ 6.0

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
9 Apr 2009CPOL2 min read 15.5K   5   2
This article shows a problem that occurs with global friend operator functions in VC++6.

Introduction

I ran into a problem while I was trying to write a small class to test how I could overload operators in C++ in VS6. I will tell you all about it in this small article, and I hope it will help you. I am writing this because in all the C++ books I have read, I haven't come across this problem. I also think this is a problem only for the C++ compiler in VS6. I tried this example in VS2008 and it worked fine.

The problem

The problem I am telling you about is about friend member functions. I tried to overload the >>, <<, and == operators as global operators. And of course, I made them friends of my class. When I compiled the program, I received an error telling me that I can't access the private members of my class from the operator functions. I triple checked that my operator functions were friends, and they were. Here is the initial code:

C++
class Integer
{
    int a;
public:
    Integer(int rv){a=rv;}
    
    
    friend int operator==(const Integer& lv, const Integer& rv);
    friend ostream& operator<<(ostream& out,const Integer& rv);
    friend istream& operator>>(istream& in,Integer& rv);
};

int operator==(const Integer& lv, const Integer& rv)
{
    cout<<"operator==()"<<endl;
    return lv.a==rv.a;
}
ostream& operator<<(ostream& out,const Integer& rv)
{
    return out<<rv.a;
}
istream& operator>>(istream& in,Integer& rv)
{
    return in>>rv.a;
}

The problem that occurs here is that in the code above, we all assume that the friend declarations specify both the declaration of the global function and the fact that this function is a friend. Apparently, in VC6, this isn't the case with operator functions. If I try to write a normal friend function, everything works fine. The code below demonstrates this:

C++
class MyClass
{
    int a;
public:
    friend void f(MyClass& mc);
};

void f(MyClass& mc)
{
    cout<<mc.a<<endl;
}

The code above doesn't generate an error even though it is almost the same as the operator definitions (they are both friends, and access the private data through a reference specified as an argument).

The solution

I solved this problem in a very simple manner. I added a declaration for the global operator functions above the class definition code. This seemed to get the job done. Here is the code I added:

C++
class Integer;
int operator==(const Integer& lv, const Integer& rv);
ostream& operator<<(ostream& out,const Integer& rv);
istream& operator>>(istream& in,Integer& rv);

and here is the code for the entire application:

C++
#include "stdafx.h"
#include <iostream>

using namespace std;

class Integer;
int operator==(const Integer& lv, const Integer& rv);
ostream& operator<<(ostream& out,const Integer& rv);
istream& operator>>(istream& in,Integer& rv);

class Integer
{
    int a;
public:
    Integer(int rv){a=rv;}
    
    friend int operator==(const Integer& lv, const Integer& rv);
    friend ostream& operator<<(ostream& out,const Integer& rv);
    friend istream& operator>>(istream& in,Integer& rv);
};

int operator==(const Integer& lv, const Integer& rv)
{
    cout<<"operator==()"<<endl;
    return lv.a==rv.a;
}
ostream& operator<<(ostream& out,const Integer& rv)
{
    return out<<rv.a;
}
istream& operator>>(istream& in,Integer& rv)
{
    return in>>rv.a;
}

int main(int argc, char* argv[])
{
    Integer i1(8),i2(4);

    if(i1==i2)
    {
        cout<<i1<<", "<<i2<<endl;
    }
    else
    {
        cin>>i1;
    }
    cout<<i1<<", "<<i2<<endl;
    return 0;
}

This is it. Please feel free to post your comments and suggestions.

History

This article was submitted on April 09, 2009.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthere is no such problem with vc++6.0 Pin
wanft9-Apr-09 3:14
wanft9-Apr-09 3:14 
GeneralRe: there is no such problem with vc++6.0 Pin
Florin Badea9-Apr-09 9:11
Florin Badea9-Apr-09 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.