我有两个类需要在同一 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
编译器给我:"变量具有不完整的类型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上的实时演示
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何在创建自定义迭代器时获得 std::p air 的第一个和第二个?
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 如何获取句子中单词的第一个字母*没有*指针,引用等
- 引用保留向量中的第一个元素
- 迭代器指针不引用第一个元素
- 取消引用结构以获取第一个成员的值
- 为什么数组的类型推断会将指向第一个的指针优先于对数组的引用
- 引用结构中的另一个结构,其中第二个结构也引用第一个结构
- 未能创建第一个库`对“main”的未定义引用
- 错误:未定义文件中的第一个引用符号
- pair.first是否返回对第一个值的引用
- 是成员声明`decltype(name)name;`在第一个名称引用封闭作用域的本地结构中允许