Tuesday, July 14, 2009

Help with c++ Huffman BTree!?

Hey guys with the folowing protoype and function,


I am getting this error, which says...





In function `void getHuffmangetHuffman(BTree*, std::string%26amp;, std::string*)':


error: invalid initialization of non-const reference of type 'std::string%26amp;'


from a temporary of type 'std::basic_string%26lt;char, std::char_traits%26lt;char%26gt;,


std::allocator%26lt;char%26gt; %26gt;'





can someone please help me out......


here is my code below!








void getHuffman(BTree* node, string%26amp; s, string sa[]);














void getHuffmangetHuffman(BTree* node, string%26amp; s,string sa[])


{


if (node != NULL)


{


if (node-%26gt;getLeftChild() == NULL %26amp;%26amp; node-%26gt;getRightChild() ==


NULL)


{


char buf[2];


buf[0] = (char)node-%26gt;getLetter();


buf[1] = 0;


string s2 = buf;


sa[node-%26gt;getLetter() - 'a'] = s2 + " = " + s;


}


getHuffman(node-%26gt;getLeftChild(), s + "1",sa);


getHuffman(node-%26gt;getRightChild(), s + "0",sa);


}


}

Help with c++ Huffman BTree!?
The second parameter of getHuffman is a parameter of type std::string%26amp;. Reference parameters need need to use actual variables in the function call. If you use a literal instead, it'll complain.





For example:


myFunction(int %26amp;nValue);





you can call it as myFunction(nX);


but not myFunction(5);


or even myFunction(nX+1);





Because 5 does not have an address, so %26amp;nValue can not be set to anything. When this happens, the compiler normally puts 5 in a temporary variable and passes the temp variable to the function instead. Same with nX+1.





You have two options:


1) make the std::string %26amp; parameter a const std::string %26amp; instead.





2) When you call the function, pass it an actual variable instead of an rvalue.

land survey

No comments:

Post a Comment