C++ C2061和x27;字符串';:未声明的标识符

C++ C2061和x27;字符串';:未声明的标识符,c++,string,identifier,C++,String,Identifier,我得到一个“字符串”:试图为我的程序构建解决方案时出现未声明的标识符错误。 我相信这与在函数声明中声明字符串类型有关。错误首先出现在添加节点的函数签名中: #include <iostream> #include <string> #include "stdafx.h" using namespace std; void addNode(struct Node *head, string text); struct Node { string info;

我得到一个“字符串”:试图为我的程序构建解决方案时出现未声明的标识符错误。 我相信这与在函数声明中声明字符串类型有关。错误首先出现在添加节点的函数签名中:

#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;
};
#包括
#包括
#包括“stdafx.h”
使用名称空间std;
void addNode(结构节点*头,字符串文本);
结构节点{
字符串信息;
串出;
节点*下一步;
};
以下是该程序的其余代码:

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;
    }
}
intmain()
{
const int width=2;//X轴上的单元格数
const int height=2;//Y轴上的单元格数
字符串网格[高度];
结构节点*列表=新节点;
结构节点*listcpy;
网格[0]=“00”;
网格[0]=“0。”;
//----------------------------------------------------------------------------------
对于(int i=0;iinfo[j]='0')//如果当前单元格是节点
{
list->out.append(to_string(i)+“”+to_string(j)+“”);//append节点坐标
if(j信息[j+1]='0'){//如果右侧有节点
list->out.append(to_string(i)+“”+to_string(j+1)+“”);
}
否则{
list->out.append(“-1-1”);
}
if(i下一步->信息[j]='0'){//如果底部有节点
列表->输出。追加(到字符串(i+1)++“”+到字符串(j)+”);
}
否则{
list->out.append(“-1-1”);
}
}
}
列表=列表->下一步;
}
while(listcpy!=NULL)
{
如果(listcpy->out!=“”)
{
下一步就要结束了;
}
}
}
}
//模仿
void addNode(结构节点*头,字符串文本)
{
Node*newNode=新节点;
新建节点->信息=文本;
newNode->next=NULL;
newNode->out=“”;
节点*cur=头部;
while(cur){
如果(当前->下一步==NULL){
cur->next=newNode;
返回;
}
cur=cur->next;
}
}

有人知道如何更正此错误吗?

很可能您已启用预编译头模式:

在这种情况下,
#include“stdafx.h”
之前的所有内容都将被忽略。不管你喜欢与否,这就是Microsoft实现预编译头功能的方式


因此,您需要禁用项目的预编译头并删除每个代码文件顶部的
#include“stdafx.h”
,或者需要确保
#include“stdafx.h”
始终是第一行(注释除外,但在任何情况下它们都不起任何作用)。(这不适用于头。)

您很可能启用了预编译头模式:

在这种情况下,
#include“stdafx.h”
之前的所有内容都将被忽略。不管你喜欢与否,这就是Microsoft实现预编译头功能的方式


因此,您需要禁用项目的预编译头并删除每个代码文件顶部的
#include“stdafx.h”
,或者需要确保
#include“stdafx.h”
始终是第一行(注释除外,但在任何情况下它们都不起任何作用)。(这不适用于头。)

摆脱
#包括“stdafx.h”
struct Node*list
如果您在Visual Studio中工作,并且在上有预编译的头文件,
#包括“stdafx.h”
必须是第一个include。这是第一条错误消息吗?您的问题可能是您的
addNode
声明在
struct Node
定义之前,因此编译器不知道函数的第一个参数类型。您可以通过将
addNode
声明移动到
struct Node之后来解决这一问题
。并去掉预编译头和
#无论如何都包括“stdafx.h”
。去掉
#包括“stdafx.h”
struct Node*list
如果您在Visual Studio中工作并且打开了预编译头,
#包括“stdafx.h”
必须是第一个include。这是第一条错误消息吗?您的问题可能是您的
addNode
声明在
struct Node
定义之前,因此编译器不知道函数的第一个参数类型。您可以通过将
addNode
声明移动到
struct Node之后来解决这一问题
。并且去掉预编译的头文件,
#无论如何都要包含“stdafx.h”