繁体   English   中英

如何在我的代码中获取相同结构的结构中的引用

[英]How to get a reference in a struct of the same struct in my code

我有这个结构,但是当我提到对它自己的类型的引用时,我得到了一些错误:

struct Point  {
              int x;
              int y;

              bool isLattice;

              Vect2D gradient;

              ///used only when isLattice is false
              Point p00; ///top-left corner
              Point p01; ///top-right corner
              Point p10; ///bottom-left corner
              Point p11; ///bottom-right corner

              ///the displacement vectors  Gradient-this.Vect2D
              Vect2D disp00;
              Vect2D disp01;
              Vect2D disp10;
              Vect2D disp11;  ///used along the gradient vector of the lattice points

              ///the Q-s for each corner Q= Disp*G which are going to be used for the interpolation
              double q00;
              double q01;
              double g10;
              double q11;

          };

我需要知道我应该如何初始化这个结构,因为这是我过去在其他代码中的做法。 这其中有什么错误?

我猜你来自托管语言

struct Point  {
    //...
    Point p00; 
    // ...
}

这是你的问题。 C++ 中的对象不是引用。 这会将 Point 的实例放入 Point 的实例中。 这将如何运作? 您可以使用指针或引用。

改变

Point p00;

Point & p00; // this requires being initialized  
             // in the constructor initialization list

或者可能

std::shared_ptr<Point> p00;

改变Point pX; Point * pX; .

这样,代码将被编译,并且您可以使用适当的构造函数初始化 Point 指针。

为什么不在 Point 实例中使用另一种类型的结构?

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM