Search code examples
c++stringidentifier

C2061 'string': undeclared identifier


I am getting a 'string': undeclared identifier error when trying to build a solution to my program. I believe it has something to do with declaring a string type in a function declaration. The error first appears in the function signature for add node:

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

here is the rest of the code for the program:

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

Does anyone know how to correct this error?


Solution

  • Most likely you have precompiled headers mode enabled:

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

    In this case everything that comes before the #include "stdafx.h" is ignored. Like it or not, this is how Microsoft implemented the precompiled header feature.

    Therefore, you either need to disable precompiled headers for your project and remove #include "stdafx.h", or you need to make sure that #include "stdafx.h" is always the first line (except for comments, but in any case they do not play any role) at the top of every code file. (This does not apply to headers.)