0

[need help][c++][beginner] x does not name a type

i was trying to implement Map class in c++ using Node and Point classes. the map represents the Cartesian coordinates system (x and y axes) so essentially it has an origin. now when i try to set a value (0,0) to the origin I get the error: ./Playground/file0.cpp:75:5: error: 'origin' does not name a type 75 | origin.setPoint(0,0); | ^~~~~~ I tried to look up the solution on stackoverflow and other websites but the situations were different than mine. for example some suggested that the classes that i'm using further in my code should be implemented in upper code lines . i tried that by putting the Node Class higher than map class in the code but nothing changed. I'm a newbie at c++ I'd be very thankful if someone could help me with this. Also if there are any general suggestions/ problems through the code I'd love to hear how i could improve. Thanks in advance the code link: https://code.sololearn.com/cdkPEhWT1f1D

10th Jan 2020, 6:56 PM
Mido M. Konar
Mido M. Konar - avatar
2 Respuestas
+ 2
You can't call a method in your class description. If you want the origin to be (0|0) by default, initialize it in a constructor. This also goes for your other classes. You should provide proper constructors that either initialize the class variables to default variables if neccessary or let you construct an object based on parameters. For example, the "Point" class could have one constructor with no parameters, initializing the coordinates to (0|0), and one that takes two integers and let's you initialize a point based on those coordinates. That way, you can also get rid of that "isReady" variable. Also, those setters in your "Node" class won't work. Variables have a scope lifetime, meaning the "Node" object you instantiate in each of those setters will be destroyed immediately after the function has finished, and the pointers will essentially be dangling pointers pointing to already destroyed objects. Dereferencing them at any time would be undefined behaviour.
10th Jan 2020, 7:37 PM
Shadow
Shadow - avatar
0
thanks for answering. much appreciated <3
11th Jan 2020, 8:00 PM
Mido M. Konar
Mido M. Konar - avatar