compiler Error???

Anyone know why I'd be getting an E0304 in visual studio for the line with the comment?

It's part of a bigger code but I don't think the rest is relevant...

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
  void HuffmanTree::encodeChar(HuffmanNode* root, string& encodedChar, map<char, string>&codeMap)
{
	if (!root)
		return;

	if (!root->left && !root->right)
	{
		char temp = root->str[0];
		codeMap.insert(temp, encodedChar);   //Error E0304
	}


}
Copy the full error message that Visual Studio gives you.

Try doing codeMap.insert({temp, encodedChar});
Last edited on
Full Error Message

E0304 no instance of overloaded function "std::map<_Kty, _Ty, _Pr, _Alloc>::insert [with _Kty=char, _Ty=std::string, _Pr=std::less<char>, _Alloc=std::allocator<std::pair<const char, std::string>>]" matches the argument list HuffmanEncoding C:\Users\hmeltz\source\repos\HuffmanEncoding\HuffmanEncoding\HuffmanTree.cpp 114
Try doing codeMap.insert({temp, encodedChar});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example program
#include <iostream>
#include <string>
#include <map>

using std::string;
using std::map;

int main()
{  
    string encodedChar = "test";
    map<char, string> codeMap;
	char temp = 'c';
	
	//codeMap.insert(temp, encodedChar); // error: no matching function for call to 'std::map<char, std::basic_string<char> >::insert(char&, std::string&)'
	codeMap.insert({temp, encodedChar});
}
thanks Ganado,
codeMap.insert({temp, encodedChar}); seemed to fix it. Can you explain why the curly brackets are necessary there?

thanks
http://www.cplusplus.com/reference/map/map/insert/
Look at the function declarations in the link above.

It either takes:
(1) a single element as a parameter
(2) a position and a single element
(3) a range of iterators
(4) an initializer_list of values

None of those match what you were trying to call, hence the compiler error.

For insert (const value_type& val);,
Member type value_type is the type of the elements in the container,
defined in map as pair<const key_type, mapped_type>

So you need to insert a pair, not individual parameters. Using { } is an easy way to achieve this without writing out the full type of pair<char, string>(mychar, mystring).
Last edited on
ok, thanks
Use emplace rather than insert:

 
codeMap.emplace(temp, encodedChar);

Topic archived. No new replies allowed.