-1

I have this struct, but I get some errors when I mention a reference to its own type:

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;

          };

I need to know how I should initialize this struct, because this is how I used to do it in the past in other codes. What is the error in this one?

Andrew Stef
  • 61
  • 1
  • 7

3 Answers3

2

I would guess you're coming from a managed language

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

This is your problem. Objects in C++ are NOT references. This puts an instance of Point inside an instance of Point. How will that work? You could use a pointer, or a reference.

Change

Point p00;

to

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

or maybe

std::shared_ptr<Point> p00;
Rob K
  • 8,757
  • 2
  • 32
  • 36
  • 2
    If you haven't already, get [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?lq=1) and forget everything you know about whatever language you already know. – Rob K Jan 27 '17 at 21:38
  • I'd pitch a `std::unique_ptr` over the `std::shared_ptr`. Doesn't seem to be any need to share ownership. – user4581301 Jan 27 '17 at 22:00
  • @user4581301 It appeared to me that the pointers were coming from somewhere else, so ownership was unclear. – Rob K Jan 29 '17 at 18:03
0

Change Point pX; to Point * pX;.

This way, the code will compile, and you get to initialise the Point pointers using an appropriate constructor.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
-1

Why dont you use another type struct in instances of Point?