2017-10-14 130 views
0

C++無效的轉換我有這樣的代碼片段:從「爲const char *」到「字符*」

void read_from_file(){ 
    ifstream fin; 
    int index=-1; 
    fin.open("eliza.dat"); 
    char line[MAX_RESP_LEN]; 
    while(fin){ 
     fin.getline(line,MAX_RESP_LEN); 
     char *ptr = 0; 
     ptr = strstr("@[email protected]",line); 
     if(strlen(line)<1){ 
      break; 
     } 
     else if(ptr!=NULL){ 
      // the next line is a keyword 
      fin.getline(line,MAX_RESP_LEN); 
      keys[++index].addword(line); 
     } 
     else{ 
      // it is a response 
      keys[index].addresp(line); 
     }  
    } 
} 

,並標有@ KWD @關鍵字的其他文件,但是當我編譯這個這個錯誤發生:

error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive] ptr = strstr("@[email protected]",line);

我已經列入本文件:

  • ostream的
  • 的iostream
  • CONIO.H
  • unistd.h中
  • time.h中
  • 的ctime
  • 文件math.h
  • stdlib.h中
  • 爲c_string
  • cstdlib
  • stdio.h
  • fstream.h

有什麼遺漏嗎?或者在這個函數之前發生錯誤?代碼有400行,所以我不能複製一切。

+0

您傳遞常數數據到'strstr',得到了一個指向常量數據備份。能夠修改它會導致問題。 – chris

回答