无效的数组分配

时间:2013-08-03 20:05:29

标签: c++ arrays project

我正在为我的C ++类做一个项目,我遇到了这个特定功能的问题。我一直得到错误“无效的数组分配,我不知道为什么。我只是尝试将位置交换为临时变量并交换回来以降序显示此信息。数组dateAdded是一个包含Chars的数组。我很感激帮助人

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; 

1 个答案:

答案 0 :(得分:1)

你说dateAdded是一个字符数组 - 然后,由于temp被声明为string,所以至少以下行会失败:

dateAdded[count+1] = temp;

使用类似

的内容
dateAdded[count+1] = temp[0];

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