Binary Tree fragment

Hello everyone.I am having trouble with my code. I'm trying to make it compile but I'm having no luck. Can someone help me compile my code? Thank you in advance.

Here is the my_btree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef MY_BTREE_H
#define MY_BTREE_H

// my_btree.h  - binary tree class (no balancing)
using namespace std;

// forward declaration of classes defined in this header
template <class T> class btree;
template <class T> class node;

template <class T> 
class btree
{
public:
  // constructors
  btree();  // no-arg constructor 
  btree(const btree & tree); // copy constructor

  ~btree(); // destructor

  // operations
  bool empty() const;
  int size() const;
  T & root() const;
  // print in-order traversal of the tree
  void print() const; 
  // insert x into tree (wherever it goes)
  void add_element(const T & x); 
  // calculate height of tree
  int height() const; 
  
protected:
  node<T> * rootnode;
  unsigned int my_size;
  
// internal methods used recursively
private: 
  // calculate height of a tree rooted at n
  int height(node<T> *n) const; 
  // print subtree rooted at n (recursive)
  void print(node<T> *n); 
  // insert x in tree rooted at node n (keep in order!)
  void insert(node<T> * n, const T & x); 
  
};

template <class T> 
class node
{

private:
  node(const T & x); // private constructor
  
  T x; // data
  node<T> * left; // left child
  node<T> * right; // right child

  friend class btree<T>;
};


#include "my_btree.cpp"

#endif 


Here is my my_btree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<string>

using namespace std;

template <class T> 
btree<T>::btree()
{

}
template <class T>
btree<T>::btree(const btree & tree)
{

}
template <class T>
btree<T>::~btree()
{

}
template <class T>
bool btree<T>::empty() const
{
return 0;
}
template <class T>
int btree<T>::size() const
{
return 0;
}
template <class T>
T & btree<T>::root() const
{
return 0;
}
template <class T>
void btree<T>::print() const
{

}
template <class T>
void btree<T>::add_element(const T & x)
{

}
template <class T>
int btree<T>::height() const
{
return 0;
}
template <class T>
unsigned int btree<T>::my_size()
{
return 0;
}
template <class T>
int btree<T>::height(node<T> *n) const
{
return 0;
}
template <class T>
void btree<T>::print(node<T> *n)
{

}
template <class T>
void btree<T>::insert(node<T> * n, const T & x)
{

}
template <class T>
node::node(const T & x)
{

}
template <class T>
btree<T>()
{

}


I get this error when i compile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
my_btree.cpp:7: error: expected constructor, destructor, or type conversion before ‘<’ token
my_btree.cpp:12: error: expected constructor, destructor, or type conversion before ‘<’ token
my_btree.cpp:17: error: expected constructor, destructor, or type conversion before ‘<’ token
my_btree.cpp:22: error: expected initializer before ‘<’ token
my_btree.cpp:27: error: expected initializer before ‘<’ token
my_btree.cpp:32: error: expected initializer before ‘<’ token
my_btree.cpp:37: error: expected initializer before ‘<’ token
my_btree.cpp:42: error: expected initializer before ‘<’ token
my_btree.cpp:47: error: expected initializer before ‘<’ token
my_btree.cpp:52: error: expected initializer before ‘<’ token
my_btree.cpp:57: error: expected initializer before ‘<’ token
my_btree.cpp:62: error: expected initializer before ‘<’ token
my_btree.cpp:67: error: expected initializer before ‘<’ token
my_btree.cpp:72: error: ‘node’ has not been declared
my_btree.cpp:72: error: ISO C++ forbids declaration of ‘node’ with no type
my_btree.cpp:77: error: expected constructor, destructor, or type conversion before ‘<’ token
Someone please help me with this part so i can start putting the rest of the information.
Hi,

not use to templates but with a few changes I did get your code to compile.

Firstly in your class decleration you have

 
    unsigned int my_size;


you just forgot the brackets it should be

 
    unsigned int mysize();


the next one is your include statement
you should use include in the .cpp file at the top

 
#include "my_btree.h" 


and get rid of the include in my_btree.h

the next one is just a syntax error in the cpp file
1
2
3
4
    node::node(const T & x)

    //should be
    node<T>::node(const T & x)


and lastly this seems to be an error so I removed It

1
2
3
4
5
template <class T>
btree<T>()
{

}


I also added an empty main functiion to get it to compile.

Hope this was helpful to you
Shredded

Thank you for your help but its still not working. I forgot to mention that i cannot alter the .h file. I still keep getting the same errors i was getting earlier. Can you still help me figure it out?
Ok.

but I would suggest you put the #include "my_btree.h" into the .cpp as it needs the class declerations.

This shouldnt cause a problem with the other include because you have
your #ifndef stuff in your header.

although the my_size function is going to cause an error without the brackets.

I wish i could put the #include "my_btree.h" into the .cpp but im not allowed to. Is there a way you can help me with the error messages? I don't think im calling them properly. Thanks.
Hi again,

have got it compiling without errors as is except for the following

my_size() - with brackets in .h file ***EDIT I think this must be a property
** You will need to delete in .cpp

node<T>::node(const T & x) - added <T> in .cpp file

and remove extra constructor at end of .cpp

to test it I used a null main function in a seperate .cpp file
as follows

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<string>

using namespace std;

#include "my_btree.h"

int	main(int argc,char** argv)
{

}


probably need to test the class but at least it compiles.

***EDIT only compile the main.cpp and not my_btree.cpp

Thats about all i can think of Good luck
Shredded

Last edited on
Awesome i got it to compile! :D thank you so much! is there a way that you can help me with the constructors? :D
Topic archived. No new replies allowed.