0

I have switched to c++ from c. I have already done this kind of thing in c (Please don't dig the rest of the code for now just see the way of calling to functionfind_two_smallest(); it's definition in c and then below in c++ it's call. definition and declaration) because the problem is just there, No where else:I have written the full code because i am mentioning the line numbers which are giving errors.

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

The definition of this function call in c is : void Huffman::find_two_smallest(struct Node **pmin1, struct Node **pmin2) { struct node *ppt, *pt = tree; struct node *min1 = tree; struct node *min2 = NULL; ppt = NULL; while(pt!=NULL) { if(pt->is_processed == 0) { if(pt->freq < min1->freq) { min2=min1; min1 = ppt; } } ppt = pt; pt = pt->next; } *pmin1 = min1; *pmin2 = min2; }

This was all in c and it works absolutely fine. Actually i am trying to find two minimum number. whereas my structure is:

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

struct node *tree;

I do the same in c++.Except that i ahave used "Node" instead of "node"There i also need to declare this find_two_smallest()function in class Huffman. That i do like this:

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

whereas definition i am doing this: I have written the line numbers to predict errors:*

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    }

Whereas function call i do the same way as i do in c. The result is largenumber of errors. I am beginner to c++ just switched from c. Any help please ? You can see the line numbers i have given above corresponding the errors Errors:

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
Sss
  • 1,519
  • 8
  • 37
  • 67
  • 1
    The error is in the way this code is put together. We'd probably need a complete example that replicates the problem. For example, from just the code presented, we can't tell if `find_two_smallest` is declared as a member function in a class called `Huffman`, or not. Also, it's hard to tell where you have `node` and where you have `Node`. – David Schwartz Feb 27 '14 at 22:30
  • Get rid of the extraneous "struct" keywords in your C++ example. Unlike C, C++ doesn't need to be convinced over and over that the type is a struct. – PaulMcKenzie Feb 27 '14 at 22:31
  • @DavidSchwartz in c++ class huffman void find_two_smallest(struct Node* *pmin1, struct Node **pmin2);)(its declaration in class) is my member function of this class Huffman.Whereas "Node" is in c++ and "node" is in c. – Sss Feb 27 '14 at 22:35
  • 1
    Your C++ paste above contains `node` all over the place. – David Schwartz Feb 27 '14 at 22:36
  • @DavidSchwartz Yes my mistake while converting c to c++. I have just edited it – Sss Feb 27 '14 at 22:43
  • 1
    The error is most likely in code not shown. Perhaps there's a missing `;` at the end of a `struct` or `class` block above the lines that are giving you errors. Perhaps there's a missing `}`. – David Schwartz Feb 27 '14 at 22:46
  • @DavidSchwartz thnaks for the help it is solved. YOU LAST COMMENT WORKED. – Sss Feb 27 '14 at 23:02

2 Answers2

2

You don't need the struct keyword in the declaration of the function in c++. It thinks you are attempting to forward declare the struct. In fact you only need the struct keywork in struct definition.

rerun
  • 25,014
  • 6
  • 48
  • 78
1

Since you didn't post a complete program, I will. Please add or remove lines to duplicate your error, or tell us what you see below that you didn't have in your program.

Note that I removed the extraneous "struct" keywords, and basically copied and pasted as much as I could of your example. And yes, it compiles with no errors in 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;
}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • even it i remove or add struct it works fine (may be you are right for different version of c++ compiler)but mine works fine even after the removal or addition of "struct". THe problem was what David mentioned in his last comment. – Sss Feb 27 '14 at 23:04