[英]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
The compiler gives me: "the variable has incomplete type rotation2d"编译器给我:“变量的类型为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;
}
}
To resolve circular dependencies in C++, forward declarations are invented for.为了解决 C++ 中的循环依赖,发明了前向声明。
Somehow OP tried it but in a wrong way.不知何故,OP 尝试过,但方式错误。
So, if因此,如果
class translation2d
needs class rotation2d
class translation2d
需要class rotation2d
and和
class rotation2d
needs class translation2d
class rotation2d
需要class translation2d
the second one has to be forward declared before the first.第二个必须在第一个之前向前声明。
struct rotation2d; // forward declaration -> incomplete type
struct translation2d {
void doSomethingWith(rotation2d rot);
};
struct rotation2d {
void doSomethingWith(translation2d trans);
};
Demo on Compiler Explorer Compiler Explorer 上的演示
A forward declaration makes anincomplete type .前向声明使类型不完整。 Incomplete types are restricted concerning what can be done with them.不完全类型在可以用它们做什么方面受到限制。
Neither the size nor the contents of an incomplete type is known.不完整类型的大小和内容均未知。 Hence, the compiler denies everything where this would be needed, eg因此,编译器拒绝所有需要这样做的内容,例如
It is allowed to use incomplete types for允许使用不完整的类型
I must admit that I was not aware of the latter but found:我必须承认,我不知道后者,但发现:
SO: Incomplete types as function parameters and return values SO:不完整的类型作为函数参数和返回值
for my enlightment.为了我的启蒙。
Please, note that parameters of function declarations may be incomplete but not parameters of function definitions .请注意,函数声明的参数可能不完整,但函数定义的参数不完整。 Hence, the second part of the fix is to make functions non- inline if incomplete types are needed in them.因此,修复的第二部分是在函数中需要不完整的类型时使函数非内联。
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
}
Demo on Compiler Explorer Compiler Explorer 上的演示
The fixed and completed sample code of OP: 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";
}
Output:输出:
tR: (-2, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.