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
7
  • 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. Commented Feb 27, 2014 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. Commented Feb 27, 2014 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. Commented Feb 27, 2014 at 22:35
  • 1
    Your C++ paste above contains node all over the place. Commented Feb 27, 2014 at 22:36
  • 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 }. Commented Feb 27, 2014 at 22:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

NO sorry this was not the reason, But thanks for trying to help me.
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;
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.