bintree object is not understanding my overloaded operators

This is a discussion on bintree object is not understanding my overloaded operators within the C++ Programming forums, part of the General Programming Boards category; Hi All, I am getting the following error regarding inserting items into a bintree: Code: terminate called after throwing an ...

  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.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Given that bintree is implemented in YOUR code, it would be encumbant upon you to identify and specify the conditions in which bintree::insert() might throw that exception, and then ensure the calling code complies with those conditions.

    If someone else gave you the code for the bintree (template) class, ask them to explain those conditions to you.

    Beyond that, the code you have supplied is large enough to reduce the likelihood anyone here will give you a specific answer - the expectation is that you will do some work to narrow down the scope of the problem before asking here.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Hi grumpy thanks for your advice - I have since shortened my original post, but I really can't figure out this problem. I know it has to do with these overloaded operators not working correctly, but I can't figure out why. It looks pretty straightforward to me...

    I can see that the following line in the binnode.h file rings true and therefore causes the error:

    Code:
    nodeData == dataItem
    But that line should be calling my operator:

    Code:
    bool mazeRow::operator==(const mazeRow &other) const
    {
       return(this->y == other.y);
    }
    And the y value is 1 with the first row, and 2 with the second... so... I can't understand why it evaluates as true?

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    I solved it! Took me a lot of staring, but it was because every time I added a row I didn't add a new row object.. I was adding the SAME object.

    So modifying the "row number" (or y variable) meant that I was modifying it for the only object I had. So comparing objects would always come back true.

    I managed to fix it by creating a new row object each time the while loop executes and storing the new one in each row of the maze. Perfect. Now I just have to figure out printing it to screen. Woot.

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    And that boys and girls is what we in the biz call a BFO. The best of us get them often.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Yeah, it was a bit of a Big Fat One <grin>
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Actually that means Blinding Flash of the Obvious

    Usually what happens about 1 second after you see a stupid mistake.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    35
    Haha I get them all the time, I love programming

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, 05: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, 01:14 AM
  4. returning a temp. object problem
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2003, 08:12 PM
  5. Replies: 5
    Last Post: 11-24-2002, 10:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21