2011-12-08 15 views
1

他の質問を検索しましたが、どれも を正確に適用していないようです。 私は迷路を通してルートを見つけるプログラムを書いています。 この問題を抱えている唯一の本物のものはこのコンパイラエラーです。 これはNode(構造体)を返す1つの関数と関係があります。ノードにタイプエラーがありません

ヘッダファイル:(私は定義するものをカットオフ)

#include <iostream> 
    #include <string> 
    using namespace std; 


    class Graph { 

    private: 

     struct Node { 
      int id; //int id 
      Node * north; //north path node 
      Node * south; //south path node 
      Node * east;  //east path node 
      Node * west;  //went path node 
      bool visited; // visited bool 
     }; 

     //this struct holds the path that is found. 
     struct Elem { 
      int id;   //The id of the node 
      string last; //the door that it passed through 
      Elem * back;  //back one path   
      Elem * next; //forward one path 
     }; 

     //This is a graph with a very smart struct 

     //This is the main node that makes up the graph. 


     Node * start; 

     Node ** initArr; 
     int arrLen; 

     Elem * head; 
     Elem * tail; 

     int path; 

    public: 

     Graph(); 
      //Constructs empty graph 

     Graph(const Graph &v); 
      //copy constructor 

     ~Graph(); 
      //destructor 

     Graph & operator = (const Graph &v); 
      //assignment operator 

     void output(ostream & s) const; 
      //Prints the graph 

     void input(istream & s); 
      //input and creates the graph 

     Node * find(int id); 
      //finds the node in the graph 

     void makePath();  
      //makes a path through the maze 

     bool findPath(Node* cur, string room); 
      //worker function for recursion 

     void pathOut(ostream & s) const; 
      //Outputs the found path 

     void removeTail(); 
      //Removes the last element 

     void addTail(Node* n, string door); 
      //Adds the element to the tail 

     //Mutators 
     void setId(Node* n ,int x); 
     void setVisited(Node* n, bool v); 



     //Elem Mutator 
     void seteId(Elem* e, int x); 

     //Elem Accessor 
     int geteId(Elem* e); 

     //Accessors 
     int getId(Node* n); 
     bool getVisited(Node* n); 




    }; 

そして、私の実際のコードファイル。

#include <iostream> 
    #include "graph.h" 
    using namespace std; 

     //Constructs empty graph 
     Graph::Graph() 
     { 
      start = 0; 
      head = tail = 0; 
      path = 0; 
     } 

     //copy constructor 
     Graph::Graph(const Graph &v) 
     { 
      //not implemented 
     } 

     //destructor  
     Graph::~Graph() 
     { 
      for(int i = 0; i < arrLen + 1; i++) 
      { 
       delete initArr[i]; 
      } 


      while(head != 0) 
      { 
      Elem* p = head; 
      head = head->next; 
      delete p; 
      } 

      delete[] initArr; 
     } 

     //assignment operator 
     Graph & Graph::operator = (const Graph &v) 
     { 
      //not implemented 
     } 

     //Prints the graph 
     void Graph::output(ostream & s) const 
     { 
      s<<"Node"<<'\t'<<"North"<<'\t'<<"East"<<'\t'<<"South"<<'\t'<<"West"<<'\n'; 
      for(int i = 1; i < arrLen + 1; i++) 
      { 
       Node* temp = initArr[i]; 
       s<<temp->id<<'\t'; 

       if(temp->north != 0) 
        s<<temp->north->id<<'\t'; 
       else 
        s<<"--"<<'\n'; 

       if(temp->east != 0) 
        s<<temp->east->id<<'\t'; 
       else 
        s<<"--"<<'\n'; 

       if(temp->south != 0) 
        s<<temp->south->id<<'\t'; 
       else 
        s<<"--"<<'\n'; 

       if(temp->west != 0) 
        s<<temp->west->id<<'\t'; 
       else 
        s<<"--"<<'\n'; 

       s<<'\n'; 


      } 

     } 

     //input and creates the graph 
     void Graph::input(istream & s) 
     { 
      int length = 0; 
      s>>length; 
      arrLen = length; 
      if(s) 
      { 

       //define array 
       initArr = new Node*[length + 1]; 
       int temp = 0; 
       for(int i = 1; i < length + 1; i++) 
       { 
        //Create node     
        s>>temp; 
        Node* n = new Node; 
        n->id = temp; 
        n->visited = false; 
        //Add to array 
        initArr[i] = n; 
       } 

       //Make Exit Node 
       Node *x = new Node; 
       x->id = 0; 
       x->visited = false; 
       initArr[0] = x; 

       //Loop through all of the node input 
       int tn = 0; 
       for(int f = 0; f < length; f++) 
       { 
        //Set Pointers 
        s>>tn; 
        Node* curNode = find(tn); 
        int n = 0; 
        int e = 0; 
        int st = 0; 
        int w = 0; 
        s>>n>>e>>st>>w; 
        curNode->north = find(n); 
        curNode->east = find(e); 
        curNode->south = find(st); 
        curNode->west = find(w); 
       } 
       //set Entry point to graph 
       int last = 0; 
       s>>last; 
       start = find(last); 

      } 
     } 

     //finds the node in the array 
     Node* Graph::find(int id) 
     { 
      if(id == 0) 
      { 
       return initArr[0]; 
      } 
      if(id == -1) 
      { 
       return 0; 
      } 
      else 
      { 
       for(int i = 1; i < arrLen + 1; i++) 
       { 
        if(initArr[i]->id == id) 
        { 
         return initArr[i]; 
        } 

       } 
       cerr<<"NOT FOUND IN GRAPH"; 
       return 0; 
      } 
     } 

     //makes a path through the maze 
     void Graph::makePath() 
     { 
      if(findPath(start->north, "north") == true) 
      { 
       path = 1; 
       return; 
      } 
      else if(findPath(start->east, "east") == true) 
      { 
       path = 1; 
       return; 
      } 
      else if(findPath(start->south, "south") == true) 
      { 
       path = 1; 
       return; 
      } 
      else if(findPath(start->west, "west") == true) 
      { 
       path = 1; 
       return; 
      } 
      return; 
     } 


     //finds a path to the outside 
     bool Graph::findPath(Node* cur, string room) 
     { 
      addTail(cur, room); 

      if(cur = initArr[0]) 
      { 
        return true; 
      } 

      if(cur->north != 0 && cur->north->visited == false) 
      { 
        cur->visited = true; 
        findPath(cur->north, "north"); 
      } 
      else if(cur->east != 0 && cur->east->visited == false) 
      { 
        cur->visited = true; 
        findPath(cur->north, "east"); 
      } 
      else if(cur->south !=0 && cur->south->visited == false) 
      { 
        cur->visited = true; 
        findPath(cur->north, "south"); 
      } 
      else if(cur->west != 0 && cur->west->visited == false) 
      { 
        cur->visited = true; 
        findPath(cur->north, "west"); 
      } 
      else 
      { 
       cur->visited = false; 
       removeTail(); 

      } 
     } 

     //Outputs the found path 
     void Graph::pathOut(ostream & s) const 
     { 
      if(path == 1) 
      { 
       Elem *p; 
       p = head->next; 

       while(p != 0) 
       { 
        s<<p->id<<"--> "<<p->last; 
        p= p->next; 
       } 
      } 
      else if(path == 0) 
      { 

      } 

     } 

     //Removes the last element in the chain   
     void Graph::removeTail() 
     { 
      Elem* temp = 0; 
      temp = tail; 
      tail = tail->back; 
      delete temp; 
     } 

     //Adds the element to the tail 
     void Graph::addTail(Node* n, string door) 
     { 
      if(head != 0) 
      { 
       Elem* temp = new Elem; 
       temp->id = n->id; 
       tail->next = temp; 
       tail->last = door; 
       temp->back = tail; 
       temp->next = 0; 
       tail = 0; 
      } 
      else 
      { 
       Elem *p = new Elem; 
       p->last = ""; 
       p->back = 0; 
       p->next = 0; 
       head = p; 
       tail = p; 
      } 

     } 


     //Mutators 
     void Graph::setId(Node *n ,int x) 
     { 
      n->id = x; 
     } 
     void Graph::setVisited(Node *n, bool v) 
     { 
      n->visited = v; 
     } 

     //Elem Mutator 
     void Graph::seteId(Elem *e, int x) 
     { 
      e->id = x; 
     } 

     //Elem Accessor 
     int Graph::geteId(Elem *e) 
     { 
      return e->id; 
     } 

     //Accessors 
     int Graph::getId(Node *n) 
     { 
      return n-> id; 
     } 
     bool Graph::getVisited(Node *n) 
     { 
      return n->visited; 
     } 


    /* 
     //This is a graph with a very smart struct 

     //This is the main node that makes up the graph. 
     struct Node { 
      int id; //int id 
      Node *north; //north path node 
      Node *south; //south path node 
      Node *east;  //east path node 
      Node *west;  //went path node 
      bool visited; // visited bool 
     }; 

     //this struct holds the path that is found. 
     struct Elem { 
      int id;   //The id of the node 
      string last; //the door that it passed through 
      Elem* back;  //back one path   
      Elem* next;  //forward one path 
     }; 

     Node* Start; 

     Node ** initArr; 

     Elem* head; 
     Elem* tail; 
     */ 

//outputs using named operation 
ostream & operator << (ostream &s, const Graph & v) 
{ 
    v.output(s); 
    return s; 
} 

エラーが検索機能で発生しています。

+0

"find関数でエラーが発生しています" - なぜここに 'Node'と' find'関数の定義以外の* anything *がありますか? – Xeo

+1

エラーを教えてください。 –

答えて

2

cppファイルでは、Nodeはグローバルスコープにありません。

Graph::Node* Graph::find(int id){ 
// ... 
} 

を関数内、あなたが再びGraphの範囲にいる、などあなたがそれを修飾する必要はありません:それはそのようなものが、あなたが戻り値の型でそれを修飾する必要があるとして、Graph内にネストされます。

+0

+1コンパイラなしでエラーを見つけたら、本当に感心しています –

1

クラスGraph内に構造体としてNodeとElementの両方が定義されています。クラスGraphの外でそれらを定義する方が良いでしょう。別個のNodeクラスを定義し、要素structをプライベートメンバーとして格納することができます。このエラーは、NodeがGraph :: privateノードとしてアクセスできるGraphのプライベートメンバーであるために発生します。例えば。グラフ::ノード* find(...)。

関連する問題