请有人能减轻我的挣扎。我正在尝试组织两个类(点)以在其方法中返回相反的类。笛卡尔点类具有返回极点的方法,反之亦然。
Point2D.h
#pragma once
#include "PointPolar2D.h"
class Point2D
{
private:
double x;
double y;
public:
Point2D(double x, double y);
PointPolar2D toPolar();
~Point2D();
};
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()
{
}
PointPolar2D.h
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
PointPolar2D.cpp
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
它不会编译。该错误说: toPolar:unknown覆盖说明符以及之前的意外 token ;
请帮我找出原因。它一定是显而易见的东西。
如果需要,我将提供任何澄清。
谢谢。
编辑过
按照@Amit的建议创建了MCVE。谢谢。
最佳答案
从类的名称,我猜PointPolar2D
是Point2D
的子类。
因此,PointPolar2D.h需要使用#include
Point2D.h。您还有:
#include "PointPolar2D.h"
在Point2D.h中。那就是循环包含。它导致各种问题。
从Point2D.h中删除该行,并添加一个前向声明。
class PointPolar2D;
您不需要完整的类定义即可声明该函数。
PointPolar2D toPolar();
向前声明就足够了。
确保在Point2D.cpp中的
#include
PointPolar2D.h。您需要PointPolar2D
的定义才能实现Point2D::toPolar
。