当从c切换到c++时,c++中带有双指针的函数声明和定义会出错

function declaration and definition with double pointer in c++ giving error when switched from c to c++

本文关键字:c++ 声明 函数 指针 出错 定义 当从      更新时间:2023-10-16

我已经从c切换到了c++。我已经在c中做了这类事情(现在请不要挖掘其余的代码,只看调用函数的方法find_two_smallest();它在c中的定义,然后在c++中的调用.定义和声明),因为问题就在那里,没有其他地方:我已经写了完整的代码,因为我提到的是给出错误的行号。

void huffman()
{
struct node *temp, *pmin1, *pmin2, *pt = tree;
while(remaining>2)
{
find_two_smallest(&pmin1, &pmin2);//please pay attention on this function
}

c中此函数调用的定义为:void Huffman::find_two_smallist(结构节点**pmin1,结构节点**pmin2){结构节点*ppt,*pt=树;结构节点*min1=树;结构节点*min2=NULL;ppt=NULL;while(pt!=NULL){如果(pt->is_processed==0){if(pt->freq<min1->freq){min2=min1;min1=ppt;}}ppt=pt;pt=pt->next;}*pmin1=min1;*pmin2=min2;}

这一切都在c中,它运行得非常好。事实上,我正在努力寻找两个最小的数字。而我的结构是:

struct node
{
unsigned int symbol;
int freq;
struct node *left, *right,*next;
int id;
int is_processed;
};
struct node *tree;

我在c++中也这样做。除了我用了"Node"而不是"Node"之外,我还需要在类Huffman中声明这个find_two_smallist()函数。我喜欢这个:

void find_two_smallest(struct Node **pmin1, struct Node **pmin2);

而定义我这样做:我已经写了行号来预测错误:*

31    void find_two_smallest(struct Node **pmin1, struct Node **pmin2)
32   {
33   struct Node *ppt, *pt = tree;
34   struct Node *min1 = tree;
35     struct Node *min2 = NULL;
36     ppt = NULL;
37     while(pt!=NULL)
38     {
39       if(pt->is_processed == 0)
40       {
41         if(pt->freq < min1->freq)
42         {
43          min2=min1;
44          min1 = ppt;
45        }
46      }
47      ppt = pt;
48      pt = pt->next;
49    }
50     *pmin1 = min1;
51     *pmin2 = min2;
52    }

而函数调用的方式与c中的相同。结果是出现大量错误。我是刚从c转换到c++的初学者。有什么帮助吗你可以看到我上面给出的与错误相对应的行号错误:

fz.c: In member function ‘void Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)’:
fz.c:33:25: error: cannot convert ‘Huffman::Node*’ to ‘Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node*’ in initialization
fz.c:34:22: error: cannot convert ‘Huffman::Node*’ to ‘Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node*’ in initialization
fz.c:39:9: error: invalid use of incomplete type ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:33:8: error: forward declaration of ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:41:11: error: invalid use of incomplete type ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:33:8: error: forward declaration of ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:41:24: error: invalid use of incomplete type ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:33:8: error: forward declaration of ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:48:11: error: invalid use of incomplete type ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:33:8: error: forward declaration of ‘struct Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node’
fz.c:50:11: error: cannot convert ‘Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node*’ to ‘Huffman::Node*’ in assignment
fz.c:51:11: error: cannot convert ‘Huffman::find_two_smallest(Huffman::Node**, Huffman::Node**)::node*’ to ‘Huffman::Node*’ in assignment

在c++中的函数声明中不需要struct关键字。它认为您正在尝试转发声明该结构。实际上,您只需要在结构定义中使用结构键即可。

由于您没有发布完整的程序,我会发布的。请添加或删除行以重复您的错误,或者告诉我们您在下面看到的程序中没有的内容。

请注意,我删除了无关的"struct"关键字,并基本上尽可能多地复制和粘贴了您的示例。是的,它在C++中编译时没有任何错误。

struct Node
{
unsigned int symbol;
int freq;
Node *left, *right,*next;
int id;
int is_processed;
};
Node *tree;
class Huffman
{
void find_two_smallest(Node **pmin1, Node **pmin2);
};
void Huffman::find_two_smallest(Node **pmin1, Node **pmin2)
{
Node *ppt, *pt = tree;
Node *min1 = tree;
Node *min2 = 0;
ppt = 0;
while(pt != 0)
{
if(pt->is_processed == 0)
{
if(pt->freq < min1->freq)
{
min2=min1;
min1 = ppt;
}
}
ppt = pt;
pt = pt->next;
}
*pmin1 = min1;
*pmin2 = min2;
}