2016-12-09 23 views
0

プログラムにソリューションを構築しようとすると、「文字列」:宣言されていない識別子エラーが発生します。 私は関数宣言で文字列型を宣言することと関係があると考えています。エラーが最初のノード追加のための関数のシグネチャに表示されます。C2061 'string':宣言されていない識別子

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

このエラーを修正する方法を誰もが知っている:ここ

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

は、プログラムのコードの残りの部分はありますか?

+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

ありがとう!これは私の問題を解決した –

関連する問題