65.9K
CodeProject is changing. Read more.
Home

Problem with global operator functions in VC++ 6.0

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1 vote)

Apr 9, 2009

CPOL

2 min read

viewsIcon

15688

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:

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:

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:

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:

#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.