1

我的代码有错误(错误 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;
    };
};

统计数据

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

我有可用的默认构造函数为什么错误

4

2 回答 2

10

这是循环依赖的经典例子。头文件Node.h依赖于stat.h哪个头文件,Node.h依此类推。

由于你只声明了一个 in 类型的指针变量stat_hNode你不需要为此包含头文件,声明类就足够了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中不再存在循环依赖。

于 2013-07-03T06:21:16.633 回答
1

代替

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

Node()
{
    mess = NULL;
    next = NULL;
};
于 2013-07-03T06:25:47.630 回答