[英]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.