Class with property of its same type

Hi, I need to handle some doors with an Arduino and I want to use classes in my program.

I want to set a link from one door object to another door then I thought to create a property with the same type of itself:

class Door{
  int nr;
  Door other;

  Door(int nr){
    this->nr=nr;
  }
  void setOther(Door other){
    this->other=other;
  }
};

Door door1=Door(1);
Door door2=Door(2);
door1.setOther(door2);
door2.setOther(door1);

Unfortunately seems not possibile to use a class as type inside the class definition, am I right?

This is the error while compiling:
error: field 'other' has incomplete type 'Door'
Do you confirm it is not possibile? Maybe this error has some other meaning.

Thanks

Easy peasy! Do it all the time!

class door {

// bla
// bla
// bla

void setOther(door* other);

door* doorPtr;
};

void door ::setOther(door* other){ doorPtr = other; }

Just pass in the address of the other door object.

You have some sort of Java background?

-jim lee

Thanks Jim, yes, I know Java and Php but I'm not skilled in C++ then I know how to write what I have in my mind but I have many lacks about the syntax (it's the same with English!)

Your code is almost clear, but why the setOther method definition is outside the class?

That's why I figured you were a Java programmer. You guys make things so complex!

You CAN leave it in the class but typically in c++ you set up two files .h the class definition and interface stuff that whomever is using it needs to see. And the .cpp file with all the methods and code in it. (The things that do stuff.)

Also, from what I've seen over the years. Typically you put the public stuff at the top where its the first thing you see. Or the first thing the user sees when trying to figure out what she can do with it. Then put the private stuff below because its really only read by you.

Lastly pointers.. Don't fear them.

int aRock; // is aRock

&aRock // is the address of aRock.

int* aRockPtr is a pointer to an int. the pointer is called aRockPtr

aRockPtr = &aRock; // Now aRockPtr is pointing to aRock.

When you create a door object..

door firstDoor; // Just like Java..

door secondDoor; // Again, easy..

firstDoor.setDoor(&secondDoor); // Pass in the address to the second door.

when inside the first door and you want to call a method of your saved second door.. (Its just a pointer) instead of myDoorPtr.something(); it is myDoorPtr->something(); Pointers objects use arrows, not dots.

I hope this helps and doesn't just make your life complicated.

-jim lee

Thank you Jim, your notes are really clear to me and makes me understand the suggestion of last post.

Unfortunately, now I have another problem with lamdas and params... but I'll open another topic!

(BTW: it looks to me C++ men are making things complex! Why so many []*symbols& ;D in java everything is an object and all the references are made as a pointer!)

OPs code is attempting to create an instance of the Door class INSIDE another instance of the same class. If possible, that would quickly recurse and consume all memory. The embedded instance would contain another instance, which would contain another instance, and so on, until there was no memory left.

You can put a POINTER to an instance within the class, and then dynamically create the instance through a begin() method or something similar. But you cannot have another instance of the class as member data.

If you are trying to represent a list of doors, use a list of Door objects, rather than making the Door
class unnecessarily complicated!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.