2013-07-03 49 views
1

我的代碼有錯誤(錯誤C2512:'節點':沒有適當的默認構造函數可用) 但我有默認的構造函數爲什麼? 代碼評論我的錯誤位置,請幫我錯誤C2512,但我有默認的構造函數可用

Node.h

#pragma once 
#include "stat.h" 
#include "Automata.h" 
#include <cstdlib> 

class Node 
{ 
    friend class Automata; 
    friend class stat_a; 
    friend stat_a* makeauto(char *str); 
    friend int main(); 
private: 
    stat_a* mess; 
    char data;//harfi ke ba in masir estefadeh mishe :) 
    Node *next;//node badi dar araye node ha class stat_a :) 
public: 
    Node() 
    { 
     mess = NULL; 
     next = NULL; 
    }; 
}; 

stat.h

#pragma once 
#include "Node.h" 
#include <iostream> 

using namespace std; 
class stat_a 
{ 
    friend class Automata; 
    friend class Node; 
    friend int main(); 
private: 
    bool is_final_stat_a;  //aya final stat_a hast ??? 
    int stat_a_num;    //shomareh halat 0,1,2,... 
    Node *last;     //akharin node dar araye node haye neshan dahande masir 
    Node *first;    //Avalin node dar araye node haye neshan dahande masir 
public: 
    void add(char d,stat_a * a)//ezafeh kardan masiri ke ba estefadeh 
    {      //az harf (char d) be halat (stat_a a) miravad 
     if(first == NULL) 
     { 
      first = new Node;//error is here 
      first->data = d; 
      first->mess = a; 
      last=first; 
     } 
     else 
     { 
      last->next = new Node ;//erorr is here 
      last=last->next; 
      last->data=d; 
      last->next=NULL; 
      last->mess=a; 
     } 
    }; 

    /***********************************************************************/ 

    void print() 
    { 
     cout<<stat_a_num<<"========> is final_stat_a : "<<is_final_stat_a<<endl; 
     Node *a; 

     a=first; 
     while(a != NULL) 
     { 
      cout<<"========> By '"<<a->data<<"' go to stat "<<a->mess->stat_a_num<<endl; 
      a=a->next; 
     } 
    }; 

    stat_a() 
    { 
     last=NULL; 
     first=NULL; 
     is_final_stat_a=false; 
    }; 

    ~stat_a(void); 
}; 

我有默認的構造函數可用,爲什麼出錯

+0

您當地的語言下次就要求對計算器的幫助。請刪除註釋,它只是一個噪音的人誰不熟悉你的語言,分心真正的問題。 – SpongeBobFan

+0

您有循環包含依賴項。這是行不通的。 – juanchopanza

+0

不要像過去那樣過分地交朋友,只在真正需要時才保留它(在99.99%的情況下永遠不會)。 –

回答

10

這是一個經典的例子循環依賴。頭文件Node.h取決於頭文件stat.h,這取決於Node.h等。

因爲只在Node聲明stat_h類型的指針變量,你並不需要包括的頭文件,它足以聲明類stat_a

#pragma once 
#include "Automata.h" 
#include <cstdlib> 

class stat_a; // Declare the class, so the compiler know there's a class by this name 

class Node 
{ 
    // ... 

private: 
    stat_a* mess; // Works because you're only declaring a pointer 

    // ... 

public: 
    // ... 
}; 

然後在stat.h頭當你包含Node.h時,不再有循環依賴。

+0

哦對不起,我的代碼沒有simicolon,但有這個錯誤 – Rezaagha

+0

@ Rezaagha更新了我的回答 –

+0

@ Joachim Pileborg,但仍在stat.h錯誤 – Rezaagha

1

更換

Node(); 
{ 
    mess = NULL; 
    next = NULL; 
} 

Node() 
{ 
    mess = NULL; 
    next = NULL; 
}; 
+0

哦原諒我我的代碼沒有simicolon,但有這個錯誤 – Rezaagha

+0

所以請編輯你的問題 – Simon

相關問題