So I created a Binary tree class and I want to separate the class definition from its member function definition by putting the class def in the .h file and the function definitions in the .cpp file. Then I would include the .h file into the .cpp file. Now I got this to all work and it works fine. the problem is when I want to put the name including guards. Because I need to include the .h file in the member function .cpp file and in another .cpp that uses the binary tree class, I have to include these guards. But for some reason, once I include them both .cpp files don't seem to "see" the contents of the header file. I'm new to using to ifndef so I'm so sure what I'm doing wrong. thank you
Here's the header file called node.h:
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include <string>
#include <vector>
#include <stdbool.h>
using std::cout;
using std::endl;
typedef std::vector<std::string> vectorString;
class BST
{
vectorString data;
BST *left, *right;
public:
// Default constructor.
BST();
// Parameterized constructor.
BST(std::string);
// Insert function.
BST* Insert(BST*, std::string);
// Inorder traversal.
void Inorder(BST*);
// PreOrder Traversal.
void PreOrder(BST*);
// PostOrder Traversal
void PostOrder(BST*);
// string slicer
std::string strSlice(std::string);
// boolean isDuplicate
bool isDuplicate(std::string, vectorString);
// print vector
void printVector(vectorString);
};
#endif
and here's the member definition file called node.cpp
#include <iostream>
//#include "node.h"
#include <string>
#include <vector>
using std::cout;
using std::endl;
// Default Constructor definition.
BST ::BST()
: data(0)
, left(NULL)
, right(NULL)
{
}
// Parameterized Constructor definition.
BST ::BST(std::string value)
{
if(data.empty()){
data.push_back(strSlice(value));
}
data.push_back(value);
left = right = NULL;
}
// String slicing function definition
std::string BST ::strSlice(std::string word){
std::string word2 = "";
word2 += word[0];
word2 += word[1];
return word2;
}
// print vector function definition
void BST ::printVector(std::vector<std::string> dataVector){
for(int i = 0; i < dataVector.size(); i ++){
cout << dataVector.at(i) << " ";
}
}
// Insert function definition.
BST* BST ::Insert(BST* root, std::string value)
{
if (!root)
{
// Insert the first node, if root is NULL.
return new BST(value);
}
// Insert data.
if (strSlice(value).compare(root->data.at(0)) > 0)
{
// Insert right node data, if the 'value'
// to be inserted is greater than 'root' node data.
cout << value << " is being put in the right node " << value << " > " << root->data.at(0) << endl;
// Process right nodes.
root->right = Insert(root->right, value);
} else if (strSlice(value).compare(root->data.at(0)) == 0) {
cout << value << " is being put in the same node " << value << " = " << root->data.at(0) << endl;
root->data.push_back(value);
}
else
{
// Insert left node data, if the 'value'
// to be inserted is greater than 'root' node data.
cout << value << " is being put in the left node " << value << " < " << root->data.at(0) << endl;
// Process left nodes.
root->left = Insert(root->left, value);
}
// Return 'root' node, after insertion.
// cout << "after insert root is " << root << endl;
return root;
}
// Inorder traversal function.
// This gives data in sorted order.
void BST ::Inorder(BST* root)
{
if (!root) {
return;
}
Inorder(root->left);
printVector(root->data);
cout << endl;
Inorder(root->right);
}
void BST::PreOrder(BST* root){
if(!root){
return;
}
root->printVector(root->data);
cout << endl;
PreOrder(root->left);
PreOrder(root->right);
}
void BST::PostOrder(BST* root){
if(!root){
return;
}
PostOrder(root->left);
PostOrder(root->right);
root->printVector(root->data);
cout << endl;
}
errors:
C:\Users\14jjo\C++ Projects\Project 0\node.cpp:12:1: error: 'BST' does not name a type
BST ::BST()
^~~
C:\Users\14jjo\C++ Projects\Project 0\node.cpp:20:1: error: 'BST' does not name a type
BST ::BST(std::string value)
^~~
and here's the class that is trying to implement the binary tree class called P0.cpp:
#include <iostream>
//#include "tree.h"
#include "cleanString.h"
#include "node.h"
#include <fstream>
#include <string>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
std::ifstream fileRead;
std::string word;
fileRead.open(argv[1]);
if(!fileRead){
cout << "this is not a file\n";
} else { cout << "this is a file\n"; }
fileRead >> word;
word = cleanString(word);
BST tree, *root = nullptr;
root = tree.Insert(root, word);
while (fileRead >> word) {
word = cleanString(word);
tree.Insert(root, word);
}
tree.Inorder(root);
fileRead.close();
return 0;
}
errors:
C:/Users/14jjo/C++ Projects/Project 0/P0.cpp:22: undefined reference to `BST::BST()'
C:/Users/14jjo/C++ Projects/Project 0/P0.cpp:23: undefined reference to `BST::Insert(BST*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
just #include "node.h" and make sure to compile all cpp files