Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我有两个类需要在同一个cpp文件中相互引用,但第一个类赢了';无法识别第二类类型的对象_C++_Class_Reference - Fatal编程技术网

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

C++ 我有两个类需要在同一个cpp文件中相互引用,但第一个类赢了';无法识别第二类类型的对象,c++,class,reference,C++,Class,Reference,编译器告诉我:“变量具有不完整的类型rotation2d” class translation2d { 公众: 双x; 双y; translation2d() { x=0; y=0; } translation2d rotateBy(rotation2d rotation)//此处发布 { translation2d copy=*此; copy=translation2d(x*rotation.cosM()-y*rotation.sinM(),x*rotation.sinM()+y*rotati

编译器告诉我:“变量具有不完整的类型rotation2d”

class translation2d
{
公众:
双x;
双y;
translation2d()
{
x=0;
y=0;
}
translation2d rotateBy(rotation2d rotation)//此处发布
{
translation2d copy=*此;
copy=translation2d(x*rotation.cosM()-y*rotation.sinM(),x*rotation.sinM()+y*rotation.cosM());
返回副本;
}
};
双kEpsilon=0.000000009;
类翻译2D;
类旋转2D
{
公众:
双余角;
双正弦角;
公众:
旋转2D()
{
余角=1;
sinAngle=0;
}
旋转2D(平移2D和方向,布尔范数)
{
余角=方向x;
sinAngle=方向y;
if(标准)
规范化();
}
双余弦()
{
返回余角;
}
双sinM()
{
返回角度;
}
双谭()
{
如果(绝对值(余角)=0.0)
返回std::numeric_limits::infinity();
其他的
return-1*std::numeric_limits::infinity();
}
返回正弦角/余角;
}
}

< C/P> > P>解决C++中的循环依赖,是为.</P>发明的。 不知怎的,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);
};

提前声明是一种有效的方法。 不完整的类型在使用它们时会受到限制

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

  • 存储分配(即,使其成为变量或成员变量)
  • 访问内容(即读/写成员变量或调用成员函数)
允许对其使用不完整的类型

  • 指针和参考(具有任何限定)
  • 函数声明的参数
我必须承认,我不知道后者,但发现:

为了我的启示

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

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";
}

#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";
}