简体   繁体   English

我有两个类需要在同一个 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

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因此,编译器拒绝所有需要这样做的内容,例如

  • allocation of storage (ie makeing a variable or member variable of it)存储分配(即创建一个变量或它的成员变量)
  • access to contents (ie read/write member variables or call member functions).访问内容(即读/写成员变量或调用成员函数)。

It is allowed to use incomplete types for允许使用不完整的类型

  • pointers and references (with any qualification)指针和引用(具有任何限定)
  • parameters of function declarations.函数声明的参数。

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)

Live Demo on coliru在coliru上进行现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:如何在同一个.cpp上声明两个类在编译时“看到”? - C++: How can I make two classes declared on the same .cpp “see” each other at compile time? 我不能让两个班级互相指向 - I can't have two classes pointing at each other 同一类的两个对象互相引用 - Two objects of same class referencing each other 两个需要互相引用的对象。 馊主意? - Two objects that need to reference each other. Bad Idea? 我是否需要为抽象类提供.cpp文件? - Do I need to have a .cpp file for an abstract class? 如果必须相互包含,我应该如何安排两个类的头文件并避免不完整的类型? - How should I arrange header files of two classes and avoid incomplete type if they have to include each other? 如果我有两个成员完全相同但名称不同的结构/类,是否有任何简单的方法可以将一个转换为另一个? - If I have two structures/classes with the exact same members, but different names, is there any easy way to convert one into the other? 两个类包含彼此的对象 - two classes contain objects of each other 在 C++ 中,如何在另一个嵌套类中使用嵌套类类型(两个嵌套类在同一个外部类下) - In C++, how can I use a nested class type in an other nested class (the two nested classes are under the same outer class) C++ .cpp 文件无法识别它使用的 .h 类? - C++ .cpp file doesn't recognize .h class it uses?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM