0

I'm creating a program with multiple files and it's not recognizing cout<< in my tnode file. Can anyone locate where the problem is? Among other errors, I get this error "cout was not declared in this scope" in my node file. My main function:

#include <iostream>
#include "bst.h"
using namespace std;



int main(int argc, char *argv[]) {
    cout<<"hi";
    bst *list = new bst();
    return 0;
}

My BinarySearchTree file:

#ifndef bst_H
#define bst_H
#include <iostream>
#include <string>
#include "tnode.h"



class bst
{
    public:

    bst()
    {
    root = NULL;
    }
void add(int key, char value) {
      if (root == NULL) {
            root = new tnode(key, value);
            return
      } else
            root->add(key, value);
            return
}






tnode *root;

};

#endif

My node file:

#ifndef tnode_H
#define tnode_H
#include <iostream>
#include <string>

class tnode
{
public:
    tnode(int key, char value)
    {
                this->key = key;
                this->value = value;
                N = 1;
                left = NULL;
                right = NULL;
                cout<<"hi";
    }

void add(int key, char value) {
      if (key == this->key)
      {
            cout<<"This key already exists";
            return;
      }
      else if (key < this->key)
       {
            if (left == NULL) 
            {
                  left = new tnode(key, value);
                  cout<<"Your node has been placed!!";
                   return;
            } 
            else
            {
                  left->add(key, value);
                  cout<<"Your node has been placed!";
                  return;
            } 
      }
       else if (key > this->key)
       {
            if (right == NULL) 
            {
                  right = new tnode(key, value);
                  cout<<"Your node has been placed!!"; return;
            } 
            else
                  return right->add(key, value);
       }
      return;
}
        tnode* left;
        tnode* right;
        int key;
        char value;
        int N;

};




#endif
user2313755
  • 25
  • 1
  • 5

2 Answers2

4

You need to do :

  using namespace std;

or

  std::cout 

in your tnode file

But using namespace std is considered bad practice, so you'd better use the second way:

std::cout<<"Your node has been placed!!";
taocp
  • 23,276
  • 10
  • 49
  • 62
2

You need to use the namespace std. Either by using namespace std (which could go in .cpp files but never in .h files, read more about why here) or by using std::cout when calling it.

Community
  • 1
  • 1
Victor Sand
  • 2,270
  • 1
  • 14
  • 32