   Template Troubles and Class Crises
  Alright, well, put simply, I'm trying to make a Linked List class to eventually use as a Map class for a program/game I'm making.I've come pretty far, considering I had almsot zero knowledge of C++ classes when I started (Java is a LOT simpler than C++, so I've had to learn a lot of syntax from how I origonally learned classes).So far I'm having a compiler error I havn't been able to fix... Also, i have no deconstructors, which are soemthing I don't really udnerstand how to do yet. The most prevelant of the two is my compiler error:F:\Main\Programming\Solice\Map.h In constructor `Map<T>::Map(T) [with T = int]':89 F:\Main\Programming\Solice\main.cpp   instantiated from here 13 F:\Main\Programming\Solice\Map.h invalid use of member (did you forget the `&' ?)  F:\Main\Programming\Solice\Makefile.win [Build Error]  [main.o] Error 1MapNode.h//Template code helpfully provided by dbzprogrammer on the GameDev.net forums #include <cstdlib>template<class T>classMapNode{private:MapNode* nextNode;T nodeData;public:MapNode( T tempData ) : nextNode(NULL)  {nodeData = tempData;}    void setNext( MapNode<T> newNext );    voidsetData( T newData );    MapNode<T> getNext();   };MapNode.cpp#include "MapNode.h"//----------------------------------------------------------------------// name:setNext()// takes:MapNode<T> -- This is a MapNode.// returns:nothing.// does:changes what node this node points too.//---------------------------------------------------------------------template<class T>void MapNode<T>::setNext( MapNode<T> newNext ){nextNode = newNext;}template<class T>MapNode<T> MapNode<T>::getNext(){return nextNode;}template<class T>voidMapNode<T>::setData( T newData ){nodeData = newData;}Map.h#include <cstdlib>#include "MapNode.h"template<class T>classMap{private:MapNode<T> head(T null);public:    Map( ) {};    Map( T data ) { head.setData(data); };};
  Hmm, I've looked at your code and all I can see is that you might want your MapNode constructor to take values by reference. I'm not sure why it would be a problem with the compiler though.   ;  Although I didn't examine your code/errors thoroughly; I noticed from a quick inspection that you are attempting to seperate both the template declaration and definition into header and source files... but for some god forsaken reason the definition and declaration must be in the same header file. I know its strange, this is one of the first most aggrivating of errors I had in C++ that was fixed almost immediately after I appended the template class' .cpp file to the header file (And of course delete the old .cpp). Hope this gets you on the right track... BTW: O yes also a side note: I'm certain it doesn't matter whether its a .cpp or a .h the template class just has to be declared and defined within the same file.   ;  As written, Map::head is a member function, not a member variable.  Obviously you cannot call a member function of a function.  Presumably Map::head should be a variable, not a function.  If so, simply remove the (T null).Σnigma   ;  Since you're new to C++, I highly recommend that you don't try to write your own linked list class and instead use the std::list<> class available in the standard library.   ;  So far none of the suggestions have fixed the problem. Oddly enough, they didn't even change the problem...I forgot to inclue the bit that's erroring in my main file aswell:Map<int> testMap(1);I'm attempting to create a Map object and declare it via the constructor that takes one argument of type T.Meow.[Edited by - KuroKitten on June 19, 2006 12:52:41 AM];Quote:Original post by KuroKittenSo far none of the suggestions have fixed the problem. Oddly enough, they didn't even change the problem...FFS, did you even *read* the suggestions? Using std::list would definitely fix the problem. It's also what sane developers, from beginners up to and including experts (in almost all situations), do. You wouldn't have written this stuff in Java, but would have instead used one of the java.util.* collections classes, right?And as mentioned, you cant "head.setData(data);", because right now "head" is defined "MapNode<T> head(T null);". That's not a MapNode<T> data member, but a member function which accepts a T and returns a MapNode<T>. Member functions don't themselves have member functions, so an error is reported.   ;  As Enigma and Zahlman pointed out, MapNode<T> head(T null); is a member function declaration.Changing it to MapNode<T> head; will make it a data member.After that, though, you still get an error:error: no matching function for call to `MapNode<int>::MapNode() This is because you've got no zero-argument constructor for class template MapNode<>.  You can either write one, or change the Map constructor from: Map( T data ) { head.setData(data); }; to: Map( T data ) : head(data) { };;Quote:Original post by ZahlmanQuote:Original post by KuroKittenSo far none of the suggestions have fixed the problem. Oddly enough, they didn't even change the problem...FFS, did you even *read* the suggestions? Using std::list would definitely fix the problem. It's also what sane developers, from beginners up to and including experts (in almost all situations), do. You wouldn't have written this stuff in Java, but would have instead used one of the java.util.* collections classes, right?And as mentioned, you cant "head.setData(data);", because right now "head" is defined "MapNode<T> head(T null);". That's not a MapNode<T> data member, but a member function which accepts a T and returns a MapNode<T>. Member functions don't themselves have member functions, so an error is reported.Yes, I read the suggestions. Perhaps I should have put a clause in my orrigonal post saying that "don't bother doing this, it's stupid" doesn't really help. As I mentioned, I'm new to classes in C++, and creating a linked list is a very good excercise. I'm fully aware of C++'s list library.I did change it from MapNode<T> head(T null) to MapNode<T> head. "So far none of the suggestions have fixed the problem" implied that I did infact try them.@Robert F: I changed "Map( T data ) { head.setData(data); };" as you said, and it did, indeed fix the problem; however, I'm not exactly sure what it does ^^" The colon I had in my MapNode file was part of some code someone else had provided me with (credit was given in the comments). I know it's got to do with inheritance, though I'm not really sure exactly how it's working. Would be very appreciated if you could explain ^^Mew.   ;  The colon indicates the beginning of a constructor initialization list.  It's initializing the head data member.Note the difference between initialization and assignment.Written as Map( T data ) : head(data) { }; it is initialization.Rewritten as:Map( MapNode<T> newHead ) { head = newHead; };would be assignment, although this second version doesn't fit with your original meaning (just written to show assignment).There are many resources on the internet about constructor initialization lists.  Let me know if this was clear, or if you need further explanation.   