2012-03-21 64 views
1

當我試圖編譯一個我創建的模板類時,我得到了「左大括號匹配前的文件末尾」。當我在Visual Studio中雙擊錯誤消息時,它會將我帶到我試圖運行代碼的「主」文件的頂部。當我進入該類的.cpp文件時,除了一個成員函數外,所有成員函數都被最小化......這讓我認爲問題出在哪裏?有沒有一種快速找到缺失支柱的方法?左括號「{」C++中的錯誤

我認爲它存在於這個特定的成員函數中的另一個原因是,在聲明的底部,當我添加閉括號(我從一個縮進像5個空格的代碼塊中添加它們)時,通常Visual Studio將它們放在正確的縮進處,然後按下Enter鍵,輸入一個並按回車等,但在這種情況下,它會停止縮進兩個「製表符」,並將繼續將它們放在與我相同的縮進處繼續鍵入「}」並按回車鍵入「}」,然後按回車鍵...

成員函數的代碼很複雜且足夠長,以至於很難完成,即使我有大約5次,我找不到缺失的地方。這有什麼竅門嗎?我可以在正確的地方看嗎?謝謝!

編輯:

因爲,說實話,這是醜陋的地獄我沒有張貼。我正在實現我的第一個真正的類,它是一個模板類 - 數組的鏈表。這是很長時間和雜亂,我可能應該有一些更好的抽象。除了一點自我意識之外,我還想你們會看看帖子並繼續前進,我知道我會被誘惑......另外注意:我沒有評論一切,因爲大部分代碼都是隻能在不同的初始條件下重複使用。

template <typename Type> 
    void PQueue<Type>::enqueue(Type element) 
    { 
     blockT *runner = listHead; //BE CAREFUL - duplicate so as not to eff with listHead - used deep 

     //case 1: if this is the first element entered 
     if (listHead == NULL) { 
      blockT *newBlock = new blockT; 
      newBlock->blockTArray = new Type[MaxElementsPerBlock]; 
      newBlock->capacity = MaxElementsPerBlock; 
      newBlock->next = NULL; 
      newBlock->head = 0; 
      newBlock->blockTArray[0] = element; 
      newBlock->tail = 1; 
      listHead = newBlock; 
     } 

     //case 2: element > blockTArray[0] 
     else if (element >= runner->blockTArray[0]) { 
      //CASE 2A 
      if (runner->tail < runner->capacity) { 
       for (int i = runner->tail; i > 0; i--) { //iterate through array 
        runner->blockTArray[i] = runner->blockTArray[i-1]; //move everything 1 to right 
       } 
       runner->blockTArray[0] = element; //insert "element" at front 
       runner->tail++; //increment tail 
      } 
      //CASE 2B 
      else { 
       blockT *newBlock = new blockT; 
       newBlock->next = runner; 
       newBlock->blockTArray = new Type[MaxElementsPerBlock]; 
       newBlock->blockTArray[0] = element; 
       newBlock->tail = 1; 
       newBlock->head = 0; 
       newBlock->capacity = MaxElementsPerBlock; 
       listHead = newBlock; 
      } 
     } 

     //case 3: if runner is less than runner array[head] and array is NOT full 
     else if (element < runner->blockTArray[0]) { 
      //TRAVERSE TO FIND END OR BLOCKTARR > ELEMENT 
      blockT *back; 
      while (true) { 
       if (runner->next == NULL || runner->blockTArray[0] <= element) break; 
       else { 
        back = runner; 
        runner = runner->next; 
       } 
      } 

      //EQUAL TO ELEMENT 
      if (runner->blockTArray[0] == element) { 
       //INSERT ON THAT ARR IF SPACE 
       if (runner->tail < runner->capacity) { 
        for (int i = runner->tail; i > 0; i--) { //iterate through array 
         runner->blockTArray[i] = runner->blockTArray[i-1]; //move 1 right 
        } 
        runner->blockTArray[0] = element; //insert "element" at front 
        runner->tail++; 
       } 
       //ELSE MAKE NEW BLOCK AND PUT 1/2 ELEMENTS ON IT 
       else { 
        blockT *newBlock = new blockT; 
        newBlock->blockTArray = new Type[MaxElementsPerBlock]; 
        newBlock->capacity = MaxElementsPerBlock; 
        newBlock->blockTArray[0] = element; 
        newBlock->tail = 1; 
        newBlock->head = 0; 
        newBlock->next = runner; //set this new block's "next" = to cell runner was pointing at 
        back->next = newBlock; //take the cell the runner was stored in (back->next) and set it to new block's address 
       } 
      } 
      //if we stopped because the next arr[0] is smaller, use ->back to place on previous cell 
      else if (runner->blockTArray[0] < element) { 
       //if element is bigger than or equal to current arr and -> isn't full, add to front of -> 
       if (element == back->blockTArray[back->tail - 1] && runner->tail < runner->capacity) { 
        for (int i = runner->tail; i > 0; i--) { //iterate through array 
         runner->blockTArray[i] = runner->blockTArray[i-1]; //move everything 1 to right 
        } 
        runner->blockTArray[0] = element; //insert "element" at front 
        runner->tail++; 
       } 

       else if (element == back->blockTArray[back->tail - 1] && runner->tail == runner->capacity) { 
        if (back->tail == back->capacity) { 
         blockT *newBlock = new blockT; 
         newBlock->blockTArray = new Type[MaxElementsPerBlock]; 
         newBlock->capacity = MaxElementsPerBlock; 
         newBlock->blockTArray[0] = element; 
         newBlock->tail = 1; 
         newBlock->head = 0; 
         newBlock->next = runner; //set this new block's "next" = to cell runner was pointing at 
         back->next = newBlock; 
        } 
        else { 
         for (int i = 0, i < back->tail; i++) { 
          if (element <= back->blockTArray[i]) { 
           for (int x = runner->tail; x >= i; x--) { //iterate through array 
            runner->blockTArray[x] = runner->blockTArray[x-1]; //move everything 1 to right 
           } 
           runner->blockTArray[i] = element; //insert "element" at front 
          } 
         } 
        } 
       } 
       else if (back->tail == back->capacity) { 
        for (int i = 0, i < back->tail; i++) { 
         if (element <= back->blockTArray[i]) { 
          blockT *newBlock = new blockT; 
          newBlock->blockTArray = new Type[MaxElementsPerBlock]; 
          newBlock->capacity = MaxElementsPerBlock; 
          newBlock->head = 0; 
          for (int x = (MaxElementsPerBlock - (i+3)), z = runner->tail; 
           x >= 0, z > i; x--, z--) { 
           newblock->blockTArray[x] = runner->blockTArray[z - 1]; 
          } 
          runner->blockTArray[i + 1] = element; 
          runner->tail = i + 2; //you're two ahead in this case since you wrote to i+1 
          newBlock->tail = i + 1; //because you're one ahead of the element you inserted 
          back->next = newBlock; 
          newBlock->next = runner; 
          break; 
         } 
        } 
       } 
      } 
      //NULL CASE if next is null but current arr isn't full, shuffle and insert here 
      else if (runner->next == NULL && runner->tail < runner->capacity) { 
       for (int i = 0; i < runner->tail; i++) { 
        if (element <= runner->blockTArray[i]) { 
         for (int x = runner->tail - 1; x > blockTArray[i]; x--) { 
          runner->blockTArray[x + 1] = runner->blockTArray[x]; 
         } 
         runner->tail++; 
        } 
       } 
      } 
      else if (runner->next == NULL && runner->tail == runner->capacity) { 
       for (int i = 0, i < back->tail; i++) { 
        if (element <= runner->blockTArray[i]) { 
         blockT *newBlock = new blockT; 
         newBlock->blockTArray = new Type[MaxElementsPerBlock]; 
         newBlock->capacity = MaxElementsPerBlock; 
         newBlock->head = 0; 
         for (int x = (MaxElementsPerBlock - (i+3)), z = runner->tail; 
          x >= 0, z > i; x--, z--) { 
          newblock->blockTArray[x] = runner->blockTArray[z - 1]; 
         } 
         runner->blockTArray[i + 1] = element; 
         runner->tail = i + 2; //you're two ahead in this case since you wrote to i+1 
         newBlock->tail = i + 1; //because you're one ahead of the element you inserted 
         runner->next = newBlock; 
         newBlock->next = NULL; 
         break; 
        } 
       } 
      } 
      } 

您可以在末尾看到double}}。相信它是否需要我花一段時間來思考並寫下這段代碼。我希望它更清潔,更專業,但我不確定最好的方式去做。抽象?除了庫存Visual Studio之外,你們還使用任何東西嗎?任何安裝的環境?我剛剛看到一篇關於「藝術風格2.02」的文章。值得嗎?感謝您的經歷眼睛這個...

+3

沒有代碼我們該如何幫忙?把它縮小到一些小的地方,然後發佈代碼。 – 2012-03-21 18:16:35

+0

那麼,代碼可能很複雜,但我們怎麼能不幫助你呢?我們所能做的只是猜測,所以...檢查缺少的分號和嵌套模板,這些模板需要''''之間有空格。 – 2012-03-21 18:16:40

+0

你應該發佈一些代碼,以便我們可以嘗試在我們這邊。 – SuperOli 2012-03-21 18:16:52

回答

4

如果你完全不知道發生了什麼變化,因爲它編譯,禁用大部分的源代碼放置

#if 0 

#endif 

周圍的代碼。測試編譯以確保錯誤消失。如果是,請減少註釋掉的代碼量並重試。它應該是一個相當快速的二進制搜索(大型模塊的5-10次迭代),用於識別違規支撐。