简体   繁体   中英

Using compare operator with two different classes objects

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.

You have a circular dependency in includes. You include prostokat.h in okrag.h and the other way round. This way one of the includes will not work( you will get empty file because of ifndef ).

To solve this add a forward declaration of the class CProstokat at the beginning of okrag.h:

class CProstokat;

Also, change signatures of your operators to get arguments by const references instead of getting them by values. This will both improve performance and facilitate using forward declaration.

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 processing procostat.h the 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 include figura.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 of figura.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 #ifndef by not processing the content of the file until the corresponding #endif at the end. Then the compiler will continue to process the file okrag.g (remember: It still has not seen a single line of actual code from prokostat.h !) and come across the friend definition of operator> which uses CProkostat . But up to now, it did not see the definition of CProkrostat , therefore it complains about the undefined identifier.

Now how to fix it? Well, the simplest fix is just noticing that prokostat.h doesn't actually reference anything from okostat.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 COkrak in prokostat.h ? And second, you'll need to recompile every file including procostat.h whenever okrak.h changes, even if the file uses nothing from okrak.h .

Therefore the correct fix is to not only remove the #include "okrak.h" from prokostat.h but additionally remove the #include "prokostat.h" from okrak.h and instead add a forward declaration for CProkostat at the beginning of that file (ie before any of its definitions). That forward declaration looks like this:

class CProkostat;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM