简体   繁体   English

由于某种原因,我的代码中出现了 [json.exception.type_error.302] 。 我知道错误是什么意思,但我不知道哪里有问题

[英]I'm getting a [json.exception.type_error.302] for some reason in my code. I know what the error means but I don't know where there's a fault

My code goes through the values of each sample in a JSON file and gives out the number of sorting comparisons, the amount of memory, and time it takes for the sorting to happen.我的代码检查了 JSON 文件中每个样本的值,并给出了排序比较的次数、memory 的数量以及排序发生所需的时间。 But for some reason the code is throwing me an error.但由于某种原因,代码给我一个错误。 Would love it if someone could help me out.如果有人可以帮助我,我会很高兴。 I'm getting a [json.exception.type_error.302] for some reason in my code.由于某种原因,我的代码中出现了 [json.exception.type_error.302] 。 I know what the error means but I don't know where there's a fault我知道错误是什么意思,但我不知道哪里有问题

To be exact, this is the error I'm getting - "libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::type_error: [json.exception.type_error.302] type must be number, but is null"确切地说,这是我得到的错误 - “libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::type_error: [json.exception.type_error.302] 类型必须是数字,但是一片空白”

#include <iostream>
#include <fstream>
#include "json.hpp"
#include "mergesort.h"
#include "insertionsort.h"
#include "quicksort.h"
#include <ctime>
#include <iomanip>

int main(int argc, char* argv[]) {
  std::ifstream file;
  file.open(argv[1]);
  nlohmann::json jsonObject;
  if (file.is_open()) {
    file >> jsonObject;
  } else {
    std::cout << "Invalid Argument" << std::endl;
  }

  clock_t mergeTime, quickTime, insertTime;
  extern int insertMemAccesses, insertCompare, mergeMemAccesses, mergeCompare, quickCompare, quickMemAccesses;
//    std::cout << jsonObject;
//    nlohmann::json jsonOutput;

  std::cout << "Sample,InsertSortTime,InsertionSortCompares,InsertionSortMemaccess,MergeSortCompares,MergeSortMemaccess,QuickSortTime,QuickSortCompares,QuickSortMemaccess";

  int arraysize = jsonObject["metadata"]["arraySize"];
  int numsamples = jsonObject["metadata"]["numSamples"];


  for (int i = 0; i <= numsamples; i++) {
    std::string sample;
    if (i < 9) {
      sample = "Sample0" + std::to_string(i+1);
    } else {
      sample = "Sample" + std::to_string(i+1);
    }

    std::vector<int> insertion;
    std::vector<int> merge;
    std::vector<int> quick;

    for(int j = 0; j <= arraysize; j++){
      insertion.push_back(jsonObject[sample][j]);
      merge.push_back(jsonObject[sample][j]);
      quick.push_back(jsonObject[sample][j]);
    }

    insertTime = clock();
    InsertionSort(&insertion);
    insertTime = clock() - insertTime;

    insertTime = ((unsigned long)insertTime)/CLOCKS_PER_SEC;

    mergeTime = clock();
    MergeSort(&merge);
    mergeTime = clock() - mergeTime;

    mergeTime = ((unsigned long)mergeTime)/CLOCKS_PER_SEC;

    quickTime = clock();
    QuickSort(&quick);
    quickTime = clock() - quickTime;

    quickTime = ((unsigned long)quickTime)/CLOCKS_PER_SEC;

    std::cout<<sample<<",";
    printf("%.6lu,%d,%d,%.6lu,%d,%d,%.6lu,%d,%d\n",insertTime,insertCompare,insertMemAccesses,mergeTime,mergeCompare,mergeMemAccesses,quickTime,quickCompare,quickMemAccesses);


    insertCompare = 0;
    insertMemAccesses = 0;
    mergeCompare = 0;
    mergeMemAccesses = 0;
    quickCompare = 0;
    quickMemAccesses = 0;

  }
  return 0;
}

Step through the code, and which line does the exception happen on?单步执行代码,异常发生在哪一行? If you don't have a way to step through, then add in some std::cerr calls throughout to see where it is failing.如果您没有办法逐步完成,请在整个过程中添加一些std::cerr调用以查看失败的位置。 I would assume that the error occurs on one of the push_back lines, most likely the first.我会假设错误发生在其中一条push_back线上,很可能是第一条。 You have j <= arraysize , which most likely goes out of bounds, so when you try to access jsonObject[sample][arraysize] this causes the error.您有j <= arraysize ,这很可能超出范围,因此当您尝试访问jsonObject[sample][arraysize]时,这会导致错误。

Also, you could probably just do quick = merge = insertion = jsonObject[sample].get<std::vector<int>>() , instead of needing the loop此外,您可能只需要quick = merge = insertion = jsonObject[sample].get<std::vector<int>>() ,而不需要循环

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道是什么导致了我的代码中的分段错误 - I don't know what is causing the segmentation fault in my code 错误 C2061 语法错误标识符——我不知道我做错了什么 - Error C2061 Syntax Error Identifier -- I don't know what I'm doing wrong 链接错误。 我不知道我错过了什么 - Linking error. I don't know what I'm missing 我在以下代码(指针)上收到 SIGSEGV 错误并且不知道我哪里出错了 - I am receiving a SIGSEGV error on the following code (pointers) and don't know where I am going wrong 运行时错误。 我知道在哪里,但我不知道为什么以及如何纠正它 - Runtime error. I know where but I don't know why and how to correct it 我不知道是什么导致了错误 (C3074) - I don't know what causing the error (C3074) 我的代码退出时出现释放后使用错误,但我不知道为什么? - My code is exiting with use-after-free error but I don't know why? 我想知道我的代码中的错误。 这是打印所有偶数的总和,直到 1 到 N - I want to know the error in my code. This is to print sum of all even numbers till 1 to N 我的代码的结果为零,我不知道为什么 - The result of my code is zero and I don't know why 我正在使用嵌套的 while 循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么 - I'm using a nested while loop to Parse a text file with multiple lines but for some reason, it only goes through the first line and I don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM