0

我正在为我的 C++ 类做一个项目,但我遇到了这个特定功能的问题。我不断收到错误“无效的数组分配,我不知道为什么。我只是想将位置交换到一个临时变量并交换回来以降序显示此信息。数组 dateAdded 是一个带有字符的数组。我感谢帮助伙计们

void repAge(){
    cout << "You selected Listing by Age" << endl;

  int i;
  int pos;
  string temp;
  bool swap;

  // Look for empty row
    pos = -1;
  // look for an empty book row (title[i][0] == '\0')
  for (i = 0; i < 20; i++) {
    if (bookTitle[i][0] == '\0') {
      pos = i;
      break;
    }
  }

  // Now pos is last available value.
 do
 {
  swap = false;
  for (int count = 0; count < (pos - 1); count++)
  {
      if (dateAdded[count] < dateAdded[count+1])
      {
          temp = dateAdded[count];
          dateAdded[count] = dateAdded[count+1];
          dateAdded[count+1] = temp;
          swap = true;
      }
  }
  } while (swap);


  for (i = 0; i< pos; i++) {

    while (bookTitle[i] != '\0') {
    // print it out
    cout << "Title: " << bookTitle[i] << endl;
    cout << "ISBN: " << isbn[i] << endl;
    cout << "Quantity-On-Hand: " << qtyOnHand[i] << endl;
    cout << "Wholesale Cost: " << wholesale[i] << endl;
    cout << endl;


      break;
    }
  }

弹出的确切错误消息是“无效的数组分配”或“从 char* 到 char 的无效转换”。

该数组被声明为 dateAdded[20][11] 并且不起作用的行是这些

   temp= dateAdded[count]; 
   dateAdded[count] = dateAdded[count+1]; 
   dateAdded[count+1] = temp; 
4

1 回答 1

1

您说dateAdded的是一个字符数组-那么,至少以下行将失败,因为temp它被声明为string

dateAdded[count+1] = temp;

使用类似的东西

dateAdded[count+1] = temp[0];

可能最好声明tempchar- 没有理由使用string临时存储char数组的元素。

于 2013-08-03T20:20:14.190 回答