2016-12-09 38 views
0

當我嘗試構建解決方案時,出現'string':未聲明的標識符錯誤。 我相信它與在函數聲明中聲明一個字符串類型有關。錯誤第一次出現在函數簽名添加節點:C2061'string':未聲明的標識符

#include <iostream> 
#include <string> 
#include "stdafx.h" 
using namespace std; 

void addNode(struct Node *head, string text); 

struct Node { 
    string info; 
    string out; 
    Node* next; 
}; 

這裏是代碼的該程序的其餘部分:

int main() 
{ 
    const int width = 2; // the number of cells on the X axis 
    const int height = 2; // the number of cells on the Y axis 
    string grid[height]; 

    struct Node *list = new Node; 
    struct Node *listcpy; 

    grid[0] = "00"; 
    grid[0] = "0."; 

    //---------------------------------------------------------------------------------- 
    for (int i = 0; i < height; i++) { 
     addNode(list, grid[i]); 
    } 

    listcpy = list; //holds pointer to beggining of list 

    for (int i = 0; i < height; i++) 
    { 
     for (int j = 0; j < width; j++) 
     { 
      if (list->info[j] == '0') //if current cell is a node 
      { 
       list->out.append(to_string(i) + " " + to_string(j) + " "); //append nodes coordinate 

       if (j < width - 1) //if right cell exists 
       { 
        if (list->info[j + 1] == '0') { //if there is node to the right 
         list->out.append(to_string(i) + " " + to_string(j + 1) + " "); 
        } 
        else { 
         list->out.append("-1 -1 "); 
        } 

        if (i < height - 1) //if bottom cell exists 
        { 
         if (list->next->info[j] == '0') { //if there is node at the bottom 
          list->out.append(to_string(i + 1) + " " + to_string(j) + " "); 
         } 
         else { 
          list->out.append("-1 -1 "); 
         } 
        } 
       } 
       list = list->next; 
      } 

      while (listcpy != NULL) 
      { 
       if (listcpy->out != "") 
       { 
        cout << listcpy->out << endl; 
       } 
       listcpy = listcpy->next; 
      } 


     } 
    } 
} 

// apending 
void addNode(struct Node *head, string text) 
{ 
    Node *newNode = new Node; 
    newNode->info = text; 
    newNode->next = NULL; 
    newNode->out = ""; 

    Node *cur = head; 
    while (cur) { 
     if (cur->next == NULL) { 
      cur->next = newNode; 
      return; 
     } 
     cur = cur->next; 
    } 
} 

有誰知道怎麼糾正這個錯誤?啓用

+6

擺脫'#include「stdafx.h」'。 –

+1

'struct Node * list' < - 'struct'在C++中不需要,因爲'struct'聲明聲明瞭一個新的類型名稱。 – crashmstr

+1

如果您在Visual Studio中工作並且已經打開了預編譯頭文件,那麼'#include「stdafx.h *'*必須是第一個包含文件。 – crashmstr

回答

4

很可能是因爲預編譯頭模式:

Project -> Settings -> C/C++ -> Precompiled Headers -> Precompiled Header: Use (/Yu)

在出現的#include "stdafx.h"被忽略之前這種情況下的一切。喜歡與否,這就是Microsoft如何實現預編譯頭部功能。

因此,您需要爲您的項目禁用預編譯頭文件並刪除#include "stdafx.h",或者您需要確保#include "stdafx.h"始終是第一行(註釋除外,但無論如何它們不起任何作用)在每個代碼文件的頂部。 (這不適用於標題。)

+0

非常感謝!這解決了我的問題 –