2015-09-17 40 views
1

我在使用數組的東西時遇到了問題。我收到以下錯誤使用cstring數組有問題

In function ‘int main(int, const char**)’: 
75: error: cannot convert ‘char*’ to ‘char (*)[81]’ for argument ‘1’                 ion(char (*)[81], OneItem*, int&, int&)’ 
In function ‘void parseInformation(char (*)[81], OneItem*, int&, in 
164: error: ISO C++ forbids comparison between pointer and integer 
166: error: incompatible types in assignment of ‘const char [2]’ to 
169: error: ISO C++ forbids comparison between pointer and integer 
174: error: ISO C++ forbids comparison between pointer and integer 
174: error: ISO C++ forbids comparison between pointer and integer 
176: error: invalid conversion from ‘char*’ to ‘char’ 

代碼沒有與行號對齊。我嘗試了多種Google搜索方式,有些東西還沒有找到解決方案。

const int MAX_CHARACTERS = 80; 
    const int MAX_INVENTORY = 12; 

    typedef char OneLine[MAX_CHARACTERS + 1]; 
    struct OneItem 
    { 
     char product[MAX_CHARACTERS + 1]; 
     int quantity; 
     float unitPrice; 
     float totalPrice; 
    }; 



    int main(const int argc, const char* argv[]) 
    { 

     OneLine fileName; 
     ifstream inFile; 

     OneLine readLine; 
     OneItem inventory[MAX_INVENTORY]; 


     int readLineIndex; 
     int structureCounter = 0; 
     int averageQuantity; 
     float averagePrice; 
     float averageTotalPrice; 

     displayIntroduction(); 

     getFileName(argc, argv, fileName); 
     if (!inFile) 
     { 
      cout << "File not found: " << fileName << endl; 
     } 
     else 
     { 
      inFile.open(fileName); 
      while(!inFile.getline(readLine, MAX_CHARACTERS, '\n').eof()) 
      { 
       if (structureCounter < MAX_INVENTORY) 
       { 
        parseInformation(readLine,inventory, readLineIndex, structureCounter); 
       } 
      } 

void parseInformation(OneLine readLine[],OneItem inventory[], int & readLineIndex, int & structureCounter) 
{ 
    int tempIndex = 0; 
    int valueCounter = 0; 
    OneLine tempArray; 
    while(readLine[readLineIndex] != '\n') 
    { 
     tempArray = "\0"; 


     while(readLine[readLineIndex] == ' ') 
     { 
      readLineIndex += 1; 

     } 
     while(readLine[readLineIndex] != ' ' && readLine[readLineIndex] != '\n') 
     { 
      tempArray[tempIndex] = readLine[readLineIndex]; 
      tempIndex += 1; 
      readLineIndex += 1; 
     } 
     if(valueCounter == 0) 
     { 
      for(int i = 0; i <= strlen(tempArray); i++) 
      { 
       inventory[structureCounter].product[i] = tempArray[i]; 
      } 
      valueCounter += 1; 
     } 
     else if(valueCounter == 1) 
     { 
      inventory[structureCounter].quantity = atoi(tempArray); 
      valueCounter += 1; 
     } 
     else 
     { 
      inventory[structureCounter].unitPrice = atof(tempArray); 
      structureCounter += 1; 
     } 



    } 
    return; 
+0

把錯誤信息進入了一個問題,純文本,而不是一個鏈接到稻毛。 – Barmar

+1

「parseInformation」的第一個參數應該是一個「OneLine」數組,但「readLine」不是一個數組。 – Barmar

+0

@Barmar現在不會使'fileName'不是一個cstring數組?它使用相同的typedef,但是當我使用'fileName'使用strncopy時,我沒有任何錯誤。 – AdversaryCSGO

回答

1

您對parseInformation的定義不對。它說,readLine應該是一個OneLine的數組,但它只需要一個OneLine。它應該是:

void parseInformation(OneLine readLine,OneItem inventory[], int & readLineIndex, int & structureCounter) 

這導致所有你得到的錯誤,因爲你調用一個OneLine,而不是一個陣列的功能。在函數內部,您將readLine[readLineIndex]與一個字符進行比較,該字符需要readLine爲字符數組,而不是數組OneLine

OneLine typedef已經使這個數組成爲一個數組,您不需要將[]添加到參數聲明中。

1

正如Barmar說你需要改變第一個參數,但有一個更的問題,在這裏:

OneLine tempArray; 
while(readLine[readLineIndex] != '\n') 
{ 
    tempArray = "\0"; 

你不能讓這樣的分配。 tempArray必須const char*類型或者你需要這樣說:

tempArray[some_index] = '\0';