Link to home
Start Free TrialLog in
Avatar of MexicanHeat
MexicanHeat

asked on

Including files

Hi,

 I'm doing a project for school and we are using a linux machine with a c++ compiler.  We're supposed to be writing our classes in two parts...a header file and then the cpp file. I was under the impression that to use functions from a class you only needed to include the header file. When i try to compile, it gives me errors, but when i include the cpp file also , it works fine. Any suggestions? My code is as follows:

**TreeNODE.H**

#ifndef TREENODE_H
#define TREENODE_H

class TreeNode
{
      private:
            int value;
            TreeNode *left;
            TreeNode *right;
      public:
            TreeNode(void);
            TreeNode(int val);
            TreeNode(int val, TreeNode *l, TreeNode *r);
            void insert(int val);
            bool search(int val);
            void print(void);
            int getValue(void);
            TreeNode *getLeft(void);
            TreeNode *getRight(void);
};

#endif

**TREENODE.CPP**

#include "TreeNode.h"
#include <stdlib.h>
#include <iostream.h>

TreeNode::TreeNode(void)
{

      left = NULL;
      right = NULL;
      value = 0;
}

TreeNode::TreeNode(int val)
{

      value = val;
      left = NULL;
      right = NULL;
}

TreeNode::TreeNode(int val, TreeNode *l, TreeNode *r)
{

      value = val;
      left = l;
      right = r;
}

void TreeNode::insert(int val)
{
      if (val < value)
      {
            if (left == NULL)
                  left = new TreeNode(val);
            else
                  left->insert(val);
      }
      else
      {
            if (right == NULL)
                  right = new TreeNode(val);
            else
                  right->insert(val);
      }
}

bool TreeNode::search(int val)
{
      if (val == value)
            return true;
      if ((val < value) && (left != NULL))
            return left->search(val);
      if ((val > value) && (right != NULL))
            return right->search(val);
      return false;
}

void TreeNode::print(void)
{
      if (left != NULL)
            left->print();
      cout << value << endl;
      if (right != NULL)
            right->print();
}

int TreeNode::getValue(void)
{
      return value;
}

TreeNode *TreeNode::getLeft(void)
{

      return left;
}

TreeNode *TreeNode::getRight(void)
{
      return right;
}

**Driver File**

#include "TreeNode.h"
#include "TreeNode.cpp"
#include <stdlib.h>
#include <iostream>

int main()
{
      TreeNode myNode;
      TreeNode myNode2(7);

                myNode.print();
}

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pb_india
pb_india

Use this in your cpp file:


#include <stdlib.h>
#include <iostream.h>

#include "TreeNode.h"  //Bring this at the end
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MexicanHeat

ASKER

You're right, thanks for the help. The problem was that originally I had been using a makefile which included the cpp for me. And now i hadn't included the cpp file. Problem solved, thanks for the quick response!