Hi

I'm trying to test my code but I keep getting some kind of strange errors

bst.hpp: In member function `void Bst<generic>::insert(generic) [with generic = int]':
test_BST.cpp:30:   instantiated from here
bst.hpp:36: error: invalid conversion from `int' to `int*'
bst.hpp:47: error: ISO C++ forbids comparison between pointer and integer
bst.hpp:52: error: ISO C++ forbids comparison between pointer and integer
bst.hpp:58: error: ISO C++ forbids comparison between pointer and integer
bst.hpp:65: error: ISO C++ forbids comparison between pointer and integer
bst.hpp:73: error: ISO C++ forbids comparison between pointer and integer
bst.hpp:79: error: ISO C++ forbids comparison between pointer and integer

here is my code

bsn.h

struct Bsn
{
  Bsn * p;          
  Bsn * l;          
  Bsn * r;         
  generic * data;   

};

bst.h

template <class generic>
class Bst
{
  public:
    Bst();
    ~Bst();
    void insert( generic x);
    void remove( generic x);
    generic & search ( generic x);
    void clear();
    bool empty();
    unsigned int size();

  protected:
    Bsn<generic> * m_root;
    Bsn<generic> * p_insert( generic x);
    Bsn<generic> * p_remove( generic x);
    Bsn<generic> * p_search( generic x);

  private:
    unsigned int m_size;

};

#include"bst.hpp"

bst.hpp ( my problem is only with the insert function everything else is fine)

template <class generic>
void Bst<generic>::insert( generic x)
{
  Bsn<generic> * temp = new Bsn<generic>;

  if( m_root == NULL)
  {
    m_root -> data = x;
    m_root -> r = NULL;
    m_root -> l = NULL;
    m_root -> p = NULL;

    m_size++;

  }

  else
  {

    if( m_root -> r == NULL && x > m_root -> data)
    {


    }
    else if( m_root -> r == NULL && x < m_root -> data)
    {



    }
    else if( m_root -> r != NULL && x < m_root -> data)
    {




    }
   else if( m_root -> r != NULL && x > m_root -> data)
    {



    }
   else
    {
      if( m_root -> l == NULL && x < m_root -> data)
      {



      }
      else if( m_root -> l != NULL && x < m_root -> data )
      {


      }

    }
  }
}

my Cppunit testing for the insert function

void Test_BST::test_insert()
{
  Bst<int>  r;

  r.insert(1);
}

Thank you for your help

Recommended Answers

All 4 Replies

void Bst<generic>::insert( generic x)

x is type "generic".

struct Bsn
{
  Bsn * p;          
  Bsn * l;          
  Bsn * r;         
  generic * data;   
};

data is type "pointer to generic".

if( m_root -> r == NULL && x > m_root -> data)

You are comparing a variable of type "generic" to a variable of type "pointer to generic" here. "generic", according to your error messages, is type integer in this case. So you are trying compare a pointer to an integer. The compiler isn't allowing you to do that.

Thank you for your replay

I tried :

void Test_BST::test_insert()
{

  Bst<int> * r;

  r.insert(1);



}

but I got


test_BST.cpp: In member function `void Test_BST::test_insert()':
test_BST.cpp:30: error: request for member `insert' in `r', which is of non-clas s type `Bst<int>*'

but I don't have clue how to fix it

Thanks in advance

Thank you for your replay

I tried :

void Test_BST::test_insert()
{

  Bst<int> * r;

  r.insert(1);



}

but I got

but I don't have clue how to fix it

Thanks in advance

I'm not too experienced with generics, but for sure that wasn't the reason you were getting the errors. I'd put it back to where it was before. Here's the problem:

m_root -> data = x;

and

x > m_root -> data

x is an integer.
m_root->data is a pointer to an integer.

You can't compare them like that. You CAN, however, dereference m_root->data and turn it into an integer:

*(m_root -> data)

Here * is the "dereference" operator. The below will be a comparison of an integer to another integer:

x > *(m_root -> data)

So this will be a legal line:

if( m_root -> r == NULL && x > *(m_root -> data))

You also need to change this line:

m_root -> data = x;

to this:

m_root -> data = &x;

Here's a little program I wrote with your skeleton to show how referencing and dereferencing can work. Like I said, I'm not too experienced with generics, so I made them integers:

#include <iostream>
using namespace std;

struct Bsn
{
  Bsn * p;          
  Bsn * l;          
  Bsn * r;         
  int * data;   
};


int main ()
{
        Bsn  a_root;
        Bsn* m_root;
        m_root = &a_root;

        int y = 6;        
        m_root->data = &y;
        int x = 7;

        if (x > *(m_root->data))
             cout << "if condition is true.\n";
        else
             cout << "if condition is false.\n";   
              
        return 0;
}

it worked :)

Thank you for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.