2016-12-09 8 views
0

내 프로그램에 대한 솔루션을 빌드 할 때 '문자열': 선언되지 않은 식별자 오류가 발생합니다. 함수 선언에 문자열 형식을 선언하는 것과 관련이 있다고 생각합니다. 프로그램의 나머지 코드 여기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++에서는 필요 없습니다. – crashmstr

+1

Visual Studio에서 작업하고 있고 미리 컴파일 된 헤더가있는 경우 '#include "stdafx.h"'* *를 첫 번째 포함해야합니다. – crashmstr

답변

4

대부분의 경우 당신이 미리 컴파일 한 헤더 모드 사용 : #include "stdafx.h"가 무시되기 전에 제공이 경우 모두에서

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

. 마찬가지로, Microsoft는 미리 컴파일 된 헤더 기능을 구현 한 것입니다.

따라서 프로젝트의 미리 컴파일 된 헤더를 비활성화하고 #include "stdafx.h"을 제거하거나 #include "stdafx.h"이 항상 첫 줄 (주석 제외하고 어떤 경우에도 역할을 수행하지 않음)인지 확인해야합니다. 모든 코드 파일의 맨 위 (이것은 헤더에는 적용되지 않습니다.)

+0

고마워요! 이게 내 문제를 해결했다. –