gpt4 book ai didi

c++ - 错误 C2512 但我有可用的默认构造函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:19:09 34 4
gpt4 key购买 nike

我的代码有错误(错误 C2512:“节点”:没有合适的默认构造函数可用)但我有默认构造函数为什么???我的错误位置在代码中注释请帮助我

节点.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;
};
};

统计.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);
};

我有可用的默认构造函数,为什么会出错

最佳答案

这是循环依赖的经典例子。头文件Node.h依赖于头文件stat.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 header 中包含 Node.h 时,不再存在循环依赖。

关于c++ - 错误 C2512 但我有可用的默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17440984/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com