【发布时间】:2012-04-01 16:38:32
【问题描述】:
我对操作员有疑问。一般来说,我知道使用运算符的基础知识。但是当我想比较两个对象时,我的问题就开始了。我有 2 个不同的类声明文件 okrag.h 和 prostokat.h 。我想比较来自不同类的两个对象:
//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
我的问题是当它开始编译并找到这一行时:friend bool operator>(COkrag o1, CProstokat o2);
上面写着:Error 1 error C2061: syntax error : identifier 'CProstokat'
我不知道是什么原因。看起来它不知道对象 CProstokat,但包含了带有此类声明的头文件。
谁能告诉我怎么了?
编辑:
我已经更正了我的代码,这就是我得到的。我不知道为什么我不能将 const 添加到 bool operator>(CProstokat & p2); 我希望它看起来像这样:
bool operator>(CProstokat & p2);
但是编译器说
Error 3 error C2662: 'CProstokat::PodajPole' : cannot convert 'this' pointer from 'const CProstokat' to 'CProstokat &'
现在它可以在没有它的情况下工作。这是我得到的。
#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
我希望我能正确理解您的建议。感谢大家花时间回答。
【问题讨论】:
-
Google 关键字词组是“forward declaration”。
-
此外,通常的约定是使用英文标识符,而不是您的母语中的标识符。同样,类名开头的“C”大部分时间都很烦人,并且不会使您的代码变得清晰(每个IDE都可以告诉您
Circle是类还是其他任何东西,不需要“CCircle”-哪个使其更难阅读)。 -
这是我的家庭作业,老师需要这种类型的 CClass 声明。但是感谢您的建议,我将在我的私人项目中使用您推荐的符号样式。
标签: c++ class operator-keyword