gpt4 book ai didi

c++ - C2061 'string' : undeclared identifier

转载 作者:行者123 更新时间:2023-11-27 22:48:00 24 4
gpt4 key购买 nike

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

#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;
}
}

有谁知道如何纠正这个错误?

最佳答案

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

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

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

因此,您需要为您的项目禁用预编译 header 并删除 #include "stdafx.h",或者您需要确保 #include "stdafx.h" 始终是每个代码文件顶部的第一行(注释除外,但无论如何它们不起任何作用)。 (这不适用于标题。)

关于c++ - C2061 'string' : undeclared identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com