我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象

I have two classes that need to reference each other in the same cpp file, but the first one won't recognize objects of the second class type

本文关键字:第一个 引用 类型 第二个 识别 对象 文件 两个 cpp      更新时间:2023-10-16

编译器给我:"变量具有不完整的类型rotation2d">

class translation2d
{
public:
double x;
double y;
translation2d()
{
x=0;
y=0;
}
translation2d rotateBy(rotation2d rotation) //issue here
{
translation2d copy=*this;
copy=translation2d(x*rotation.cosM()-y*rotation.sinM(), x*rotation.sinM() + y*rotation.cosM());
return copy;
}
};
double kEpsilon = 0.000000009;
class translation2d;
class rotation2d
{
public:
double cosAngle;
double sinAngle;
public:
rotation2d() 
{
cosAngle=1;
sinAngle=0;
}
rotation2d(translation2d& direction, bool norm)
{
cosAngle=direction.x;
sinAngle=direction.y;
if(norm)
normalize();
}
double cosM()
{
return cosAngle;
}
double sinM()
{
return sinAngle;
}
double tanM()
{
if(abs(cosAngle)<kEpsilon)
{
if(sinAngle>=0.0)
return std::numeric_limits<double>::infinity();
else
return -1*std::numeric_limits<double>::infinity();
}
return sinAngle/cosAngle;
}
}

为了解决C++中的循环依赖关系,正向声明是为而发明的。

不知怎么的,OP尝试了,但方式不对。

所以,如果

  • class translation2d需要class rotation2d

  • class rotation2d需要class translation2d

第二个必须在第一个之前正向声明。

struct rotation2d; // forward declaration -> incomplete type
struct translation2d {
void doSomethingWith(rotation2d rot);
};
struct rotation2d {
void doSomethingWith(translation2d trans);
};

编译器资源管理器上的演示

正向声明使类型不完整。不完整的类型被限制在可以用它们做什么方面。

不完整类型的大小和内容都未知。因此,编译器拒绝所有需要的东西,例如

  • 存储分配(即生成变量或其成员变量(
  • 对内容的访问(即读/写成员变量或调用成员函数(

允许对使用不完整类型

  • 指针和引用(具有任何限定条件(
  • 函数声明的参数

我必须承认,我不知道后者,但发现:
SO:作为函数参数和返回值的不完整类型
用于我的启发。

请注意,函数声明的参数可能不完整,但函数定义的参数则不完整。因此,修复的第二部分是,如果函数中需要不完整的类型,则使函数内联。

struct rotation2d; // forward declaration -> incomplete type
struct translation2d {
void doSomethingWith(rotation2d rot);
};
struct rotation2d {
void doSomethingWith(translation2d trans)
{
trans; // will be processed somehow
}
};
// now both types are complete
void translation2d::doSomethingWith(rotation2d rot)
{
rot; // will be processed somehow
}

编译器资源管理器上的演示


OP:的固定和完整样本代码

#include <iostream>
#include <limits>
#include <cmath>
class rotation2d; // forward declaration
class translation2d
{
public:
double x;
double y;
translation2d()
{
x=0;
y=0;
}
translation2d(double x, double y): x(x), y(y) { }
translation2d rotateBy(rotation2d rotation); //issue here fixed
};
double kEpsilon = 0.000000009;
class rotation2d
{
public:
double cosAngle;
double sinAngle;
public:
rotation2d() 
{
cosAngle=1;
sinAngle=0;
}
rotation2d(const translation2d& direction, bool norm)
{
cosAngle=direction.x;
sinAngle=direction.y;
if(norm)
normalize();
}
double cosM()
{
return cosAngle;
}
double sinM()
{
return sinAngle;
}
double tanM()
{
if(abs(cosAngle)<kEpsilon)
{
if(sinAngle>=0.0)
return std::numeric_limits<double>::infinity();
else
return -1*std::numeric_limits<double>::infinity();
}
return sinAngle/cosAngle;
}
void normalize()
{
const double len = std::sqrt(cosAngle * cosAngle + sinAngle * sinAngle);
cosAngle /= len; sinAngle /= len;
}
};
// both types complete now -> circular dependency resolved
translation2d translation2d::rotateBy(rotation2d rotation)
{
translation2d copy=*this;
copy=translation2d(x*rotation.cosM()-y*rotation.sinM(), x*rotation.sinM() + y*rotation.cosM());
return copy;
}
int main()
{
translation2d t(1.0, 2.0);
rotation2d r(translation2d(0.0, 1.0), false);
translation2d tR = t.rotateBy(r);
std::cout << "tR: (" << tR.x << ", " << tR.y << ")n";
}

输出:

tR: (-2, 1)

coliru上的实时演示

相关文章: