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();
}

MexicanHeatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>When i try to compile, it gives me errors, but when i include the cpp file also , it works fine

You need to tell the compiler to use 'TreeNode.cpp' also when compiling, e.g.

g++ -o test DriverFile.cpp TreeNode.cpp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pb_indiaCommented:
Use this in your cpp file:


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

#include "TreeNode.h"  //Bring this at the end
0
pb_indiaCommented:
You cannot just compile with header file.
You need to compile with C++ file as well. The compliler needs to know this file
0
MexicanHeatAuthor Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.