-3

I have some troubles with the following code

#include<iostream>
#include<iomanip>
#include<fstream>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<functional>
#include<algorithm>
using namespace std;
struct node
{

    int weight;
    unsigned char value;
    const node * child0;
    const node *child1;
    node(unsigned char c=0,int i=-1){
        value=c;
        weight=-1;
        child0=0;
        child1=0;
    }

    //construct new internal node  that has children c1 and c2
    node (const node* c0,const node *c1){
        value=0;
        weight=c0->weight+c1->weight;
        child0=c0;
        child1=c1;


    }

    bool operator<(const node &a) const {

        return weight<a.weight;


    }
    void traverse(char * code=" " ) const;

};

void node::traverse(char * code ) const
{
    if(child0)
    {

        child0->traverse(code +'0');
        child1->traverse(code +'1');


    }
    else
    {

        cout<<" "<<value <<"     ";
        cout<<setw(2)<<weight;
        cout<<"  "<<code<<endl;

    }


}

void count_chars(int *counts)
{
    for (int i=0;i<256;i++)
        counts[i]=0;

    //ifstream file( "input.data");
    //if(!file){
        //cerr<<" couldnt open input file!\n";
    //throw "abort";

    //file.setf(ios::skipws);
unsigned char c;
    while(true)
    {

        cin>>c;
        if(c){
            counts[c]++;
        }
        else
             break;



    }


}
int main()
{

int counts[256];
count_chars(counts);
priority_queue<vector<node>,greater<node> >q;
for(int i=0;i<256;++i)
    if(counts[i])
        q.push(node(i,counts[i]));
while(q.size()<1)
{
    node *child0=new node(q.top());
    q.pop();
    node *child1=new node(q.top());
    q.pop();
    q.push(node(child0,child1));
}



cout<<" char Symbol code "<<endl;
q.top().traverse();

    return 0;
}

But it shows me some errors. For example unknown size of priority_queue and so on. Here is also the error list

1>------ Build started: Project: HUffman_coding, Configuration: Debug Win32 ------
1>  HUffman_coding.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\queue(212): error C2039: 'value_type' : is not a member of 'std::greater<_Ty>'
1>          with
1>          [
1>              _Ty=node
1>          ]
1>c:\program files\microsoft visual studio 10.0\vc\include\queue(212): error C2146: syntax error : missing ',' before identifier 'value_type'
1>c:\program files\microsoft visual studio 10.0\vc\include\queue(212): error C2065: 'value_type' : undeclared identifier
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C3203: 'less' : unspecialized class template can't be used as a template argument for template parameter '_Pr', expected a real type
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C2955: 'std::less' : use of class template requires template argument list
1>          c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(121) : see declaration of 'std::less'
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C2133: 'q' : unknown size
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(100): error C2512: 'std::priority_queue' : no appropriate default constructor available
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(103): error C2663: 'std::priority_queue<_Ty,_Container,_Pr>::push' : 2 overloads have no legal conversion for 'this' pointer
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(104): error C2662: 'std::priority_queue<_Ty,_Container,_Pr>::size' : cannot convert 'this' pointer from 'std::priority_queue' to 'const std::priority_queue<_Ty,_Container,_Pr> &'
1>          Reason: cannot convert from 'std::priority_queue' to 'const std::priority_queue<_Ty,_Container,_Pr>'
1>          Conversion requires a second user-defined-conversion operator or constructor
1>c:\users\daviti\documents\visual studio 2010\projects\huffman_coding\huffman_coding\huffman_coding.cpp(104): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have also recognized that, when I am trying to do like this q.top().traverse() after top. it does not show me traverse option. What is wrong?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 4
    If you're going to make us look at relatively large pieces of code, at least make sure it's all properly formatted and indented. – Bart Mar 14 '12 at 16:31
  • @Bart i see,but i can't format more then this,how can i format? –  Mar 14 '12 at 16:33
  • 1
    Use spaces, not tabs. (4 spaces for 1 tab/level of indentation is a reasonable rule). And remove unnecessary white-space. You get a preview of what it will look like while editing. Use it. – Bart Mar 14 '12 at 16:35

1 Answers1

3

I think that instead of

priority_queue<vector<node>,greater<node> >q;

you meant to write:

priority_queue<node, vector<node>, greater<node> >q;

That should solve the errors you're seeing at the moment.

And like I suggested, please do properly format your code. It will make everything so much easier to read and probably makes receiving answers more likely.

Bart
  • 19,692
  • 7
  • 68
  • 77