获取“错误C2039:'value_type':不是'std :: greater< _Ty>'的成员”

时间:2012-03-14 16:17:52

标签: c++ stl huffman-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;
}

但它显示了一些错误。例如,priority_queue的未知大小等等。这也是错误列表

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 ==========

我也认识到,当我试图在此之后做q.top().traverse()之后。它没有显示我的遍历选项。有什么问题?

1 个答案:

答案 0 :(得分:2)

我认为不是

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

你打算写:

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

这应该可以解决您目前看到的错误。

就像我建议的那样,请正确格式化您的代码。它将使所有内容更容易阅读,并可能使得更有可能接收答案。