2016-12-09 36 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

非常感谢!这解决了我的问题 –