I have a problem with operators. Generally I know basics of using operators. But my problem starts when I want to compare two objects. I have 2 different classes declaration files okrag.h and prostokat.h . I want to compare two objects from different classes:
//okrag.h -- circle class
#ifndef __OKRAG_H__
#define __OKRAG_H__
#include "figura.h"
#include "prostokat.h"
class COkrag: public CFigura
{
protected:
int m_iR;
public:
COkrag();
COkrag(int x, int y, int r);
~COkrag();
void ZmienR(int r);
float PodajObwod();
float PodajPole();
int PodajR();
void operator+(int r);
friend void operator+(COkrag o, int x[2]);
bool operator>(COkrag o2);
friend bool operator>(COkrag o1, CProstokat o2);
bool operator<(COkrag o2);
bool operator>=(COkrag o2);
bool operator<=(COkrag o2);
friend ostream& operator << (ostream &wy, COkrag &O);
};
#endif
//prostokat.h
#ifndef __PROSTOKAT_H__
#define __PROSTOKAT_H__
#include "figura.h"
#include "okrag.h"
class CProstokat: public CFigura
{
protected:
int m_iSz, m_iWy;
public:
CProstokat();
CProstokat(int x, int y, int szer, int wys);
~CProstokat();
void ZmienSz(int x);
void ZmienWy(int y);
float PodajObwod();
float PodajPole();
void operator+(int a);
friend void operator+(CProstokat p, int x[2]);
bool operator>(CProstokat p2);
bool operator<(CProstokat p2);
bool operator>=(CProstokat p2);
bool operator<=(CProstokat p2);
friend ostream& operator << (ostream &wy, CProstokat &P);
};
#endif
My problem is when it starts compiling and and finds this line: friend bool operator>(COkrag o1, CProstokat o2);
It says: Error 1 error C2061: syntax error : identifier 'CProstokat'
I have no idea what the reason is. It looks like it doesn’t know object CProstokat but header file with declaration of this class was included.
Can anyone tell me what’s wrong?
EDIT:
I have corrected my code and here is what I get. I don’t know why I couldn’t add const to bool operator>(CProstokat & p2); I wanted it to look like this:
bool operator>(CProstokat & p2);
but compiler says
Error 3 error C2662: 'CProstokat::PodajPole' : cannot convert 'this' pointer from 'const CProstokat' to 'CProstokat &'
Now it works without it. Here is what I get.
#ifndef __OKRAG_H__
#define __OKRAG_H__
#include "figura.h"
class CProstokat;
class COkrag: public CFigura
{
protected:
int m_iR;
public:
COkrag();
COkrag(int x, int y, int r);
~COkrag();
void ZmienR(int r);
float PodajObwod();
float PodajPole();
int PodajR();
void operator+(int r);
friend void operator+(COkrag o, int x[2]);
bool operator>(const CProstokat & p2); //I have changed it because now I can use 'this->'
bool operator<(CProstokat & p2);
bool operator>=(CProstokat & p2);
bool operator<=(CProstokat & p2);
bool operator>(COkrag o2);
bool operator<(COkrag o2);
bool operator>=(COkrag o2);
bool operator<=(COkrag o2);
friend ostream& operator << (ostream &wy, COkrag &O);
};
#endif
#ifndef __PROSTOKAT_H__
#define __PROSTOKAT_H__
#include "figura.h"
class COkrag;
class CProstokat: public CFigura
{
protected:
int m_iSz, m_iWy;
public:
CProstokat();
CProstokat(int x, int y, int szer, int wys);
~CProstokat();
void ZmienSz(int x);
void ZmienWy(int y);
float PodajObwod();
float PodajPole();
void operator+(int a);
friend void operator+(CProstokat p, int x[2]);
bool operator>(COkrag& o2);
bool operator<(COkrag& o2);
bool operator>=(COkrag& o2);
bool operator<=(COkrag& o2);
bool operator>(CProstokat p2);
bool operator<(CProstokat p2);
bool operator>=(CProstokat p2);
bool operator<=(CProstokat p2);
friend ostream& operator << (ostream &wy, CProstokat &P);
};
#endif
I hope I understand Your advice correctly. Thank all of you for time spent on answering.
The problem is that the header files include each other. This will result in the following scenario:
Assume some code file includes
procostat.h. Now when processingprocostat.hthe preprocessor will first process the include guard, defining__PROKOSTAT_H__(side node: All identifiers containing double underscores are reserved for the implementation; don’t use them for your own code), then includefigura.h, and then find and process the#include "okrag.h". In processing that file, the same will happen until the#include "prokostat.h", which instructs the compiler to again process that file (note that until that point, none of your actual code, except for the contents offigura.h, has been processed). While processing (again) that file, it will again first process the include guard, find that__PROKOSTAT_H__is already defined, and therefore honor the#ifndefby not processing the content of the file until the corresponding#endifat the end. Then the compiler will continue to process the fileokrag.g(remember: It still has not seen a single line of actual code fromprokostat.h!) and come across the friend definition ofoperator>which usesCProkostat. But up to now, it did not see the definition ofCProkrostat, therefore it complains about the undefined identifier.Now how to fix it? Well, the simplest fix is just noticing that
prokostat.hdoesn’t actually reference anything fromokostat.h, therefore you just can remove the#include "okostat.h"from it. That will break the circular dependency and therefore fix the immediate problem.However it’s not yet the perfect fix, for two reasons: First, what if you ever need to reference
COkrakinprokostat.h? And second, you’ll need to recompile every file includingprocostat.hwheneverokrak.hchanges, even if the file uses nothing fromokrak.h.Therefore the correct fix is to not only remove the
#include "okrak.h"fromprokostat.hbut additionally remove the#include "prokostat.h"fromokrak.hand instead add a forward declaration forCProkostatat the beginning of that file (i.e. before any of its definitions). That forward declaration looks like this:This lets the compiler know that a class of that name exists without telling any of the details (it’s called an incomplete type). You don’t need those details to declare the friend function (however you do need them to define the friend function, so in the corresponding implementation file you’ll have to
#include "prokostat.h").As a side note, your comparison operators should take the objects by const reference, to avoid unnecessary copying.