Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Including files

Posted on 2004-11-04
4
Medium Priority
?
322 Views
Last Modified: 2013-11-18
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();
}

0
Comment
Question by:MexicanHeat
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 12497547
>>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
 
LVL 2

Expert Comment

by:pb_india
ID: 12497566
Use this in your cpp file:


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

#include "TreeNode.h"  //Bring this at the end
0
 
LVL 2

Assisted Solution

by:pb_india
pb_india earned 100 total points
ID: 12497584
You cannot just compile with header file.
You need to compile with C++ file as well. The compliler needs to know this file
0
 

Author Comment

by:MexicanHeat
ID: 12497832
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question