我的代码有错误(错误C2512:'Node':没有合适的默认构造函数可用) 但我有默认构造函数为什么??? 我的错误位置在代码中注释请帮帮我
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 :(得分:10)
这是循环依赖的经典例子。标头文件Node.h
取决于头文件stat.h
,该文件取决于Node.h
,依此类推。
由于您只在stat_h
中声明了Node
类型的指针变量,因此您不需要包含头文件,这足以声明该类{ {1}}:
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:
// ...
};
的{{1}}标题中,不再存在循环依赖。
答案 1 :(得分:1)
替换
Node();
{
mess = NULL;
next = NULL;
}
带
Node()
{
mess = NULL;
next = NULL;
};