2011-07-12 31 views
0

我正在編寫一個學校項目的編譯器,這個任務要求我打印文本文件的標記到控制檯窗口。我想說清楚我不要想爲我完成我的功課。什麼會導致調試斷言錯誤。具體來說,在我的代碼

我一直在處理這個迭代遍歷文件的愚蠢函數,並將一個char或c-string值(我的導師在他的這部分指令中含糊不清......)連接到一個名爲「token 「。我可以通過罰款的第一行,這是「main()」,但是當我嘗試訪問下一行時,我得到兩個錯誤中的一個。第一種是字符串下標超出範圍錯誤,但我認爲這是因爲我試圖訪問不存在的字符串數組的一部分。最普遍的錯誤我得到的是一個調試斷言錯誤:

Debug Assertion Failed Final.exe File:f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c Expression: (unsigned)(c+1) <= 256

我已經包括了我的功能和其相關的頭文件。除了函數調用之外,沒有任何事情正在進行。如果可能的話,你能否看到我無法看到的東西。我意識到我的代碼結構很差,我不會撒謊(畢竟我在學校)。所以,任何評論,批評和建議都非常受歡迎。並且總是,任何時候都感謝你。

.cpp文件(就像現在)

#include <iostream> 
    #include <string> 

    using namespace std; 

    void tokenWork::openFile() 
    { 
     fileName = "test.txt"; 

     source.open(fileName); 

     if(!source.is_open()) 
     { 
     cout << "Cannot find file " << endl; 
     } 
    } 

    void tokenWork::traveseLine() 
    { 

    pos = 0; 
    while (!source.eof()) 
    { 
      getline(source,myLine); 
      int length = myLine.length(); 
      letters = new char[length]; 
      myLine.copy(letters,length); 

      c = letters[pos]; 

      if (isalpha(c)) 
      token = token + myLine[pos]; 
      else if (isdigit(c)) 
      token = token + letters; 
      else 
      { 
      cout << token << endl; 
      token = ""; 
      } 

      if (c == '{' || c == '}' || c == '+' || c == '=' || myLine[pos] == '(' || c == ')' || c == ';') 
       cout << myLine[pos] << endl; 
      c = letters[pos++]; 
    } 
} 

.h文件中

#ifndef H_LEX 
    #define H_LEX 

    #include <string> 
    #include <iostream> 
    #include <fstream> 

    using namespace std; 

    class tokenWork 
    { 
    public: 
    std::string fileName; 
    std::string myLine; 
    std::string token; 

    int pos; 
    int length; 
    int c; 

    char *letters; 

    ifstream source; 

    void openFile(); 
    void traveseLine(); 
    void closeFile(); 

}; 

    #endif 
+0

我的建議是學會使用調試器。在你的函數中設置一個斷點並通過它進行調試,以確切查看導致錯誤的原因。 – Chad

+0

這條線是幹什麼的? if(c =='{'|| c =='}'|| c =='+'|| c =='='|| myLine [pos] =='('|| c ==') '|| c ==';')。 – kmdent

+0

你能提供你的雙線輸入嗎? – Arkadiy

回答

1

如果你只是要打印每個給定線記號,我對所有非常困惑你正在做的額外工作。收縮你的功能下降(和小的變化)應該讓你開始:

// note, poorly named function, it traverses the whole file 
void tokenWork::traveseLine() 
{ 

pos = 0; 
while (!source.eof()) 
{ 
    getline(source,myLine); 

    int len = myLine.size(); 

    // NOTE: This was missing from your code, it traverses the line 
    //  that was read in with getline() above. 
    for(int x = 0; x < len; ++x) 
    { 
     // NOTE: This is (in my opinion) a slightly more readable 
     //  version of your if() statement above on tokens 
     //  It doesn't have all your tokens, additional ones 
     //  can be added by adding a case for them above the 
     //  line that prints them out. Since there is no break 
     //  statement, the functionality for all the cases above 
     //  fall through so they all get printed out. 
     switch(myLine[x]) 
     { 
      case '{': 
      case '}': 
      case '+': 
      case '=': 
      // add additional tokens as case statements as necessary 
      cout << myLine[x] << endl; // print it out 
      break; 
      default: // not a token 
      break; 
     } 
    } 
} 
+1

這看起來不錯,但請使用size_t來存儲myLine.size()(編譯器會抱怨64位平臺,有理由),或者更好但const const size_t – stijn

+0

足夠真實 - 是爲了與OP保持舒適的等級,並且didn他們不想通過太多的類型(可能還沒有看到)。請注意,如果len更改爲size_t,則for循環中的x應該也是如此。 – Chad

2

:調試斷言錯誤從調用std::isalpha/isdigit,當你用一個值> 255,這是最大的價值傳遞給它的參數起源類型char(您可能應該在此處使用而不是int)可以保留。我不能告訴你確切的起源,儘管你不提供源文件,但你應該能夠很容易地找出它:在調試器下運行該程序,它將在斷言處斷開。在調用堆棧中上移並檢查變量的值,這應該給你一個線索。

幾個祕訣:

    你使用C++這裏
  • ,無需使用原始字符數組。改爲使用std :: string。
  • 請勿使用namepsace std;在頭文件中:包含該文件的所有內容都將導入整個std名稱空間
  • 如果包含在頭文件中,則不需要再將其包含在源文件中
  • 學會使用調試器,它是非常寶貴的工具並將幫助您瞭解爲什麼出現問題
0

在代碼中指出特定位置很困難 - 它有這麼多問題...

你看到的大部分錯誤可能是由

token = token + letters; 

字母的結果是不是空終止(字符串::複製不提供空終止,並且沒有預留空的空間反正),所以運營商倒下並遇到一些令人討厭的事情。

除此之外,它看起來像你增加pos大約每getline一次,並永遠不會重置它。你的代碼看起來應該是內部循環,但我沒有看到任何內容。

1

你忘了

#include <cctype> 

爲了使用ISDIGIT(),因而isalpha()

c = letters[pos++]; 

看起來像一個錯誤的來源。 pos永遠增加,但它永遠停止?如果它到達最後一個字符(letters.length() - 1)會發生什麼,並且您碰到這一行:增量?陣列越界我懷疑。

相關問題