当从c切换到c++时,c++中带有双指针的函数声明和定义会出错
function declaration and definition with double pointer in c++ giving error when switched from c to c++
我已经从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;
}
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别