【问题标题】:terminate called after throwing an instance of 'std::invalid_argument' what(): dataItem already in tree Abort (core dumped)在抛出“std::invalid_argument”实例后调用终止 what(): dataItem 已经在树中 Abort (core dumped)
【发布时间】:2014-05-29 15:50:06
【问题描述】:

我收到了错误

在抛出一个实例后调用终止 'std::invalid_argument'what(): dataItem 已经在树 Abort (core 倾倒)

我已经尝试了所有方法,但我想不出任何可以解决此问题的方法 下面是我的代码 - 我也使用 bintree 和 binNode :

#include <iostream>
#include <string>
#include <ctype.h>
#include <fstream>
#include <stdlib.h>
#include "bintree.h"
#include "binnode.h"
using namespace std;

class mazePoint    
{
  private:
  int x;
  char pointType;

  public:

   void setPointMaze(char ch, int i)
   {
       pointType = ch;
       x = i;
   }

   bool operator == (const mazePoint &other) const
   {
      return(this->x == other.x);
   }
   bool operator < (const mazePoint &other) const
   {
      return(this->x < other.x);
   }
};

class mazeRow
{
   private:
   bintree<mazePoint> points;
   int y;

   public:

   void setMazeRow(int rowNumber)
   {
      y = rowNumber;
   }

   bool operator==(const mazeRow &other) const
   { 
      return(this->y < other.y);
   }

   bool operator<(const mazeRow &other) const
   {
      return(this->y < other.y);
   }

   void storePointMaze(char ch, int i)
   {
      mazePoint point;
      point.setPointMaze(ch,i);
      points.insert(point);
   }

   void incrementeRow()
   {
     y++;
   }
};

void loadMaze(bintree<mazeRow> &maze, const char *filein, int argc);
int main (int argc, char* argv[])
{
   unsigned int  start = 0;
   unsigned int  finish = 0;
   bintree<mazeRow> maze;
   string filein;
   loadMaze(maze, argv[1], argc); 
   return 0;
}

void loadMaze(bintree<mazeRow> &maze, const char *filein, int argc)
{

   if ( argc < 2) {
      cout << "Must supply 1 argument to this program";
      exit(0);
   } 
    mazeRow row;
   ifstream infile(filein);      

   char temp;
   unsigned int i = 0;

   if (infile.is_open() && infile.good()) {
        while (!infile.eof())
        {
            infile.get(temp);
            i++;
      row.storePointMaze(temp, i);
      if(temp == '\n') ./
      {         
         maze.insert(row);
         row.incrementeRow();
      }
         }

   }
   else {
      cout << "Failed to open file..";      
   }
   infile.close();
}

谢谢

【问题讨论】:

  • 请使用调试器来识别导致错误的行(或至少是函数)
  • 猜测是bintree(无论 that 是什么)正在抛出异常,因为您尝试存储的某些值已经包含在其中。我们不知道bintree 是什么,所以很难说。建议将try/catch 包裹在points.insert(point) 周围并记录冲突的值。这应该会给你一个线索。

标签: c++ tree


【解决方案1】:

错误消息表明抛出了一个未在您的代码中捕获的异常。这很可能是在binTree 实现中抛出的,当您尝试插入其键已经存在的元素时?

您可以使用调试器在引发异常的地方中断(在 gdb 中 catch throw 将是命令),然后您可以检查为什么会出现这种情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多