The .cpp file below compiles successfully if I do not include the header file btree.h. However I would like to put header file separately. Can someone review the header file and see what I am missing? If I use the header file, it gives me several errors during compilation (examples of errors):
1>------ Build started: Project: Project17, Configuration: Debug Win32 ------
1>btree.cpp
1>C:source\repos\Project17\btree.h(6,12): error C3646: 'val': unknown override specifier
1>C:\source\repos\Project17\btree.h(6,12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\source\repos\Project17\btree.h(9,17): error C2061: syntax error: identifier 'string'
1>C:\source\repos\Project17\btree.h(18,21): error C2061: syntax error: identifier 'string'
1>C:\source\repos\Project17\btree.h(23,44): error C2061: syntax error: identifier '
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
//btree.h file. I would like to use a separate header file
#include <string>
class Bnode
{
public:
string val;
Bnode* pLeft;
Bnode* pRight;
Bnode(string s);
};
class Btree
{
public:
Btree();
void print();
void insert(string s);
private:
Bnode* root;
void print_subtree(Bnode* p);
Bnode* insert_at_subtree(Bnode* p, string s);
};
//******* THE BELOW IS THE .CPP FILE. NOTE .CPP is functional at this time
//******* without the header file. But I would like to use the header file
#include "btree.h"
#include <iostream>
#include <string>
using namespace std;
class Bnode
{
public:
string val;
Bnode* pLeft;
Bnode* pRight;
Bnode(string s) { val = s; pLeft = pRight = nullptr; }
};
class Btree
{
public:
Btree()
{
root = nullptr;
}
void print()
{
print_subtree(root);
}
void insert(string s)
{
root = insert_at_subtree(root, s);
}
private:
Bnode* root;
void print_subtree(Bnode* p);
Bnode* insert_at_subtree(Bnode* p, string s);
};
void Btree::print_subtree(Bnode* p)
{
if (!p)
{
return;
}
print_subtree(p->pLeft);
cout << p->val << endl;
print_subtree(p->pRight);
}
Bnode* Btree::insert_at_subtree(Bnode* p, string s)
{
if (!p)
{
return new Bnode(s);
}
if (s < p->val)
{
p->pLeft = insert_at_subtree(p->pLeft, s);
}
else if (s > p->val)
{
p->pRight = insert_at_subtree(p->pRight, s);
}
return p;
}
int main()
{
Btree my_tree;
string s;
while (true)
{
cout << "Enter a name: ";
getline(cin, s);
if (s == "")
{
break;
}
my_tree.insert(s);
}
my_tree.print();
cout << endl;
return 0;
}
|