#include "Node.h"
template <class T>Node<T>::Node(void)
{
cout << "Node created with default value\n";
_val = 0; //default
left = NULL;
right = NULL; //init the kids
}
template <class T>Node<T>::~Node(void)
{
}
template <class T>Node<T>::Node( T val )
{
//cout << "Node created with val " << val << "\n";
_val = val;
right = NULL;
left = NULL; //init the kids
}
In my application code (another .cpp file), I am trying to find a node in my tree and store it:
Node<int>* n = tree->find( val );
This line causes: error LNK2019: unresolved external symbol "public: class Node<int> * __thiscall BST<int>::find(int)" (?find@?$BST@H@@QAEPAV?$Node@H@@H@Z) referenced in function _main error.
You CAN have the template class definition and implementation separated!
Try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//: node.h
#if !defined _NODE_H_
#define _NODE_H_
template <class T>Node
{
//: Code for your class.....
};
//Then do this: before the endif directive include your .cpp file...
#include "Node.cpp"
#endif //: ~
1 2 3 4
//: Node.cpp
//: The definition code goes here but do not include "Node.h" in this source file
There are some other things to do to keep the implementacion and definition separeted with templates but some depends on the compiler:
Including cpp files is evil on this forum. Sorry. Decent reasons why as well. Anyone here care to explain?
I personally consider most evil features to be useful, but that's only because I generally don't mess them up catastrophically (I have safeguards against it). However, it's very easy to mess them up, and the time spend debugging an error caused by the may very well exceed the time saved by using that feature.
It's good to know that that feature is provided, but I have read that including .cpp files in headers is bad practice in many different areas, so for this project I'll stick to doing everything in headers.
Another question - about operator overloading.
I have overloaded the << operator as follows in my CustomClass class:
Indeed, tried this and it works. Thanks. Not sure I understand the notation, with the ostream parameters and all. Though it makes sense - since the usual << is a bitwise operation, and doesn't deal with io streams.
Now why is it that when I create a Node object with:
1 2 3 4 5 6 7 8 9 10 11 12 13
BST<CustomClass> cbst;
CustomClass cc( "abc" );
CustomClass cc2( "d" );
CustomClass cc3( "efg" );
CustomClass cc4( "sdybvsdyb" );
CustomClass cc5( "sudhsduhsudh" );
cout << cc << "\n";
cbst.add( new Node<CustomClass>( cc ) );
cbst.add( new Node<CustomClass>( cc2 ) );
cbst.add( new Node<CustomClass>( cc3 ) );
cbst.add( new Node<CustomClass>( cc4 ) );
cbst.add( new Node<CustomClass>( cc5 ) );
my output uses values based on the default constructor for the CustomClass objects instead of the strings assigned in the overloaded constructor? The cout << cc << "\n"; works fine, prints abc to the screen. The cbst prints, however, print the value assigned from the default constructor of the CustomClass objects. Anyone know why this is?
Edit: I think I need to pass these in by reference, but why?
Constructors don't overload (in the sense that a virtual method is overloadable). You either create an object of the derived type or the base type. The appropriate constructor is called.
I don't understand. What output you getting? And what does CustomClass do? Or is CustomClass only a placeholder
For output I was seeing only the last CustomClass object that I added value print for EACH of the node objects.
The CustomClass is a class I write that simply has a string variable, initialized in the constructor to the strings I pass in when creating the object. I have fixed my problem by passing the CustomClass object by reference to the Node constructor.