I am incrementally implementing a binary tree class and so far I have only the constructor
Here is my header file (named binary_tree.h):
---------------------------------------------------------------------------------
#ifndef MY_BINARYTREE_XXX
#define MY_BINARY_TREE_XXX
#include <cstdlib>
namespace my_binary_tree
{
template <class Item>
class binary_node
{
public:
//Typedefs and Constants
typedef Item value_type;
//Constructor
binary_node(const Item& init_data);
//Constant Member Functions
And here is my implementation file (named binary_tree.cpp):
---------------------------------------------------------------------------------
namespace my_binary_tree
{
The error message I get when I compile with the command:
"g++ -c binary_tree.h binary_tree.cpp"
is
"binary_tree.cpp:5: error: expected constructor, destructor, or type conversion before '<' token. "
Line 5 of the binary_tree.cpp file is:
"binary_node<Item>::binary_node(const Item& init_data)"
A template function needs to be in the header. I would recommend making a tree that can only hold a primitive data type first (like int), and then try to template it later.
Your node needs node pointers, not Item pointers: binary_node<Item> *left_branch;
After taking into consideration the helpful advice of you two, my code compiles into a .o.
I do have another issue, though. I created a driver to test whether the constructor works. It looks like this:
--------------------------------------------------------------------------------------------------
#include "binary_tree.h"
using namespace std;
using namespace my_binary_tree; //the namespace where binary_tree.h lives
int main()
{
binary_node<int> blah(4);
return 0;
}
--------------------------------------------------------------------------------------------------
When I compile the driver with the command:
"g++ -o driver binary_tree.h binary_tree.o driver.cpp"
I get a linking error.
When I remove the "using namespace my_binary_tree;" line from my driver code (because i'm not sure whether that namespace comes along with the include "binary_tree.h"), I get these errors:
driver.cpp:5: error: binary node was not declared in this scope
driver.cpp:5: error: expected primary expression before 'int'
driver.cpp:5: error: expected ';' before 'int'