C++ 在另一个类的函数中返回一个类,反之亦然,编译错误(递归)
有人能帮我减轻痛苦吗。我试图组织两个类(点),以在它们的方法中返回相反的类。笛卡尔点类具有返回极点的方法,反之亦然 Point2D.hC++ 在另一个类的函数中返回一个类,反之亦然,编译错误(递归),c++,visual-c++,C++,Visual C++,有人能帮我减轻痛苦吗。我试图组织两个类(点),以在它们的方法中返回相反的类。笛卡尔点类具有返回极点的方法,反之亦然 Point2D.h #pragma once #include "PointPolar2D.h" class Point2D { private: double x; double y; public: Point2D(double x, double y); PointPolar2D toPolar(); ~Point2D(); };
#pragma once
#include "PointPolar2D.h"
class Point2D
{
private:
double x;
double y;
public:
Point2D(double x, double y);
PointPolar2D toPolar();
~Point2D();
};
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
Point2D.cpp
#include "stdafx.h"
#include "Point2D.h"
#include "PointPolar2D.h"
Point2D::Point2D(double x, double y) : x(x), y(y)
{
}
PointPolar2D Point2D::toPolar()
{
return PointPolar2D(1, 4);
}
Point2D::~Point2D()
{
}
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
PointPolar2D.h
#pragma once
#include "PointPolar2D.h"
class Point2D
{
private:
double x;
double y;
public:
Point2D(double x, double y);
PointPolar2D toPolar();
~Point2D();
};
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
PointPolar2D.cpp
#include "stdafx.h"
#include "Point2D.h"
#include "PointPolar2D.h"
Point2D::Point2D(double x, double y) : x(x), y(y)
{
}
PointPolar2D Point2D::toPolar()
{
return PointPolar2D(1, 4);
}
Point2D::~Point2D()
{
}
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
它不编译。错误为:toPolar:unknown override说明符以及前面的意外标记强>
请帮我找出原因。这一定是什么明显的或不是。
如果需要,我将提供任何澄清。
谢谢
已编辑
按照@Amit的建议创建MCVE。谢谢。从类的名称来看,我猜
PointPolar2D
是Point2D
的一个子类
因此,PointPolar2D.h需要#包括Point2D.h。你还有:
#include "PointPolar2D.h"
在Point2D.h中。这就是圆形夹杂。它会导致各种各样的问题
从Point2D.h中删除该行并添加一个正向声明
class PointPolar2D;
声明函数不需要完整的类定义
PointPolar2D toPolar();
提前声明就足够了
确保#在Point2D.cpp中包含PointPolar2D.h。您需要定义PointPolar2D
来实现Point2D::toPolar
您可能会遇到递归包含的问题:@R即使没有提供解决此问题的所有类。谢谢你抽出时间+1.