First things first. Writing a linked list for learning purposes is a very sensible and worthwhile thing to do. However, once you've written said linked list implementation throw it away. The C++ standard library already contains an efficient, error-free linked list class: std::list. When you come to actually wanting to
use a linked list instead of learning how to write one use the standard version (#include <list>).
On to your problems. Java and C++ differ in how they handle variables. Java uses reference semantics, with the exception of the primitive types, which means everything is a reference and the code:
Object object1 = new Object();
Object object2 = object1;
results in the variables object1 and object2 referencing the same piece of memory. In C++ you have the choice between using value semantics and reference semantics. In C++ the code:
Object object1;
Object object2 = object1;
results in the variables object1 and object2 being two separate pieces of memory with equivalent value. The alternative code:
Object * object1 = new Object();
Object * object2 = object1;
delete object2;
is more similar to the Java code and the variables object1 and object2 reference the same piece of memory. Note one fundamental difference between C++ and Java though - in C++ you have to do memory management yourself and explicitly delete what you new.
Now lets look at your code:
class MapNode{ private: MapNode nextNode; Texture nodeTexture; public: MapNode(Texture tempTexture);};
The first problem is that MapNode contains another MapNode
by value. This is impossible in much the same way as storing a 1m×1m×1m metal box inside another 1m×1m×1m metal box is impossible. Even worse the MapNode that is inside this MapNode will contain a further MapNode, which will contain yet another MapNode, ad infinitum.
The solution is to use reference semantics:
class MapNode{ private: MapNode * nextNode; Texture nodeTexture; public: MapNode(Texture tempTexture);};
To continue the metal box analogy this is like having a 1m×1m×1m metal box which contains a piece of paper describing where to find another 1m×1m×1m metal box, which is obviously easier to do than storing identical boxes inside each other.
You may have seen the pointer there, remembered about memory management and wondered where to put a delete statement. This is where you must be most careful. Every object that was allocated by new must be deallocated by delete
exactly once. Deallocate it zero times and you have a memory leak. Deallocate it more than once and you have undefined behaviour, which, if you're lucky, means you program will crash immediately or, if you're unlucky, will mean that you program will appear to work perfectly for a long time until the one point where you absolutely
need it to work, at which point it will crash.
In general you should try to work out which piece of code
owns a particular object which was allocated by new and this piece of code should be solely responsible for deleteing that object. In this case that piece of code is probably your linked list implementation. Even more generally you should try to avoid manual memory management at all by using prewritten classes that handle memory for you like the standard container classes (including the aforementioned std::list).
You said you also had a problem with your compiler complaining about the Texture class. Unlike Java, C++ won't go searching through all your source files to find what a class is. When it compiles MapNode it needs to know the structure of a Texture so that is can build MapNode big enough to fit a Texture inside. In order to give it enough information about Texture you need to #include "Texture.h" before the MapNode class definition (i.e. at the start of MapNode.h).
Σnigma