Thread: bintree object is not understanding my overloaded operators

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    35

    bintree object is not understanding my overloaded operators

    Hi All,

    I am getting the following error regarding inserting items into a bintree:

    Code:
    terminate called after throwing an instance of 'std::invalid_argument'
      what():  dataItem already in tree
    Abort (core dumped)
    This error pops up when trying to insert a SECOND item.

    Here are my cpp file's classes:

    Code:
    using namespace std;
    
    class mazePoint
    {
       private:
       
       int x;
       char point_type;
          
       public:   
       
       void set_maze_point(char point, int i);
       const string toString() const;
       bool operator!=(const mazePoint &other) const;
       bool operator==(const mazePoint &other) const;
       bool operator<(const mazePoint &other) const;
    };
    
    class mazeRow
    {
       private:
       
       bintree<mazePoint> points;
       int y;
          
       public:   
       
       void set_row_number(int row_number);   
       void store_points(string line);
       void new_row();
       void clear();
       const string toString() const;
       bool operator!=(const mazeRow &other) const;
       bool operator==(const mazeRow &other) const;
       bool operator<(const mazeRow &other) const;
    };
    
    class maze
    {
       private:
       
       bintree<mazeRow> mazeRows;
             
       public:   
       
       void add_row(mazeRow &row);
       void print_maze();
    };

    And here are my overloaded operators for those classes:

    Code:
    bool mazePoint::operator==(const mazePoint &other) const 
    {
       return(this->x == other.x);
    }
    
    bool mazePoint::operator<(const mazePoint &other) const
    {
       return(this->x < other.x);
    }
    
    bool mazeRow::operator==(const mazeRow &other) const
    {
       return(this->y == other.y);
    }
    
    bool mazeRow::operator<(const mazeRow &other) const
    {
       return(this->y < other.y);
    }
    Here is the code which deals with adding rows to my "maze":

    Code:
    string filein;
       
       mazeRow row;
       row.set_row_number(1);
       
       ifstream fin;
       string line;
       //char ch;
       int i = 0;
      
       filein = arg;
      
       //open stream to filein
       fin.open(filein.c_str());
       if (!fin) 
       {
          cerr << "Unable to load maze " << filein << " \n";
          exit(0); 
       }
      
       while (!fin.eof())
       {
          i++;
          char ch[256];
          fin.getline(ch, 256);
          row.store_points(ch);
          //THIS IS WHERE THE PROGRAM HAS THE ERROR (ON 2ND RUN)
          myMaze.add_row(row);
          cout << "Printing what we have so far:\n";
          myMaze.print_maze();
          row.new_row();
       }
      
       fin.close();
    Code:
    void mazeRow::store_points(string line)
    {
       mazePoint point;
       int i = 1;
       while(i <= line.size())
       {      
          point.set_maze_point(line[i], i);
          points.insert(point);
          i++;
       }
    }


    Now my cpp file uses header files as part of the assignment which I'm not allowed to change. Here are the insert functions and overloaded operators:

    Binnode.h
    Code:
          void insert(const dataType& dataItem) {
             if (nodeData == dataItem) {
                throw std::invalid_argument("dataItem already in tree");
             }
          
             if (dataItem < nodeData) {
                if (left == NULL) {
                   left = new binNode(dataItem);
                } else {
                   left->insert(dataItem);
                }
             } else {
                if (right == NULL) {
                   right = new binNode(dataItem);
                } else {
                   right->insert(dataItem);
                }
             }
          }
       
          /********************************************************\
             overloaded operators
          \********************************************************/
    
          // overloaded dereference operator
          const dataType& operator * () const {
             return nodeData;
          }
    
          binNode<dataType>& operator = (const binNode<dataType> &other) {
             // remove current subtrees
             if (left != NULL) delete left;
             if (right != NULL) delete right;
    
             // make subtrees equal to subtree of other
             if (other.left != NULL) {
                left = new binNode<dataType>(*(other.left));
             }
             else {
                left = NULL;
             }
             if (other.right != NULL) {
                right = new binNode<dataType>(*(other.right));
             }
             else {
                right = NULL;
             }
    
             // make nodedata equal nodedata of other
             nodeData = other.nodeData;
          }

    Bintree.h
    Code:
          void insert(const dataType& newData) {
             if (root == NULL) {
                root = new binNode<dataType>(newData);
             } else {
                root->insert(newData);
             }
             numItems++;
          }
               
          /*******************************************************\
             overloaded operators 
          \*******************************************************/
    
          bintree<dataType>& operator = (const bintree<dataType> &other) {
             if (root != NULL) {
                delete root;
                numItems = 0;
             }
             if (other.root != NULL) {
                root = new binNode<dataType>(*(other.root));
                numItems = other.numItems;
             }
             return *this;
          }
    This error is making no sense!! I have clarified the operators for the nodes, so there shouldn't be any problem with it adding a second element, should there?? I'm tearing my hair out here.

    Any help with this issue would be greatly appreciated! Thanks!
    Last edited by abrownin; 05-22-2010 at 03:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloaded operators
    By jccharl in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2004, 06:14 PM
  2. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 12:08 AM
  3. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM
  4. returning a temp. object problem
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2003, 09:12 PM
  5. Replies: 5
    Last Post: 11-24-2002, 11:05 PM