bool HuffmanNode::Compare::operator()(const HuffmanNode &n1, const HuffmanNode &n2) const
{
if (n1.frequency == n2.frequency) {
return lessThan ? n1.character < n2.character : n1.character >= n2.character;
} else {
return lessThan ? n1.frequency < n2.frequency : n1.frequency >= n2.frequency;
}
}
bool HuffmanNode::Compare::operator()(const HuffmanNode *n1, const HuffmanNode *n2) const
{
return operator()(*n1, *n2);
}
在这里,我具有另一个对象(HuffmanNode)中的比较对象的比较函数。 我想通过模板将compare函数传递给另一个对象:
HeapQueue<HuffmanNode, HuffmanNode::Compare> queue;
这似乎不起作用,我什至尝试使用运算符功能。
什么是正确的格式?谢谢。
这是我的编译器错误
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1826:31: error:
no matching constructor for initialization of 'HuffmanNode'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1718:18: note:
in instantiation of function template specialization
'std::__1::allocator<HuffmanNode>::construct<HuffmanNode>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1561:14: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::__construct<HuffmanNode>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1030:25: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::construct<HuffmanNode>' requested here
__alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1121:9: note:
in instantiation of member function 'std::__1::vector<HuffmanNode,
std::__1::allocator<HuffmanNode> >::__construct_at_end' requested here
__construct_at_end(__n);
^
./HeapQueue.hpp:25:26: note: in instantiation of member function
'std::__1::vector<HuffmanNode, std::__1::allocator<HuffmanNode> >::vector'
requested here
VectorCompleteTree() : V(1) {}
^
./HeapQueue.hpp:46:7: note: in instantiation of member function
'VectorCompleteTree<HuffmanNode>::VectorCompleteTree' requested here
class HeapQueue
^
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit copy
constructor) not viable: requires 1 argument, but 0 were provided
class HuffmanNode {
^
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit move
constructor) not viable: requires 1 argument, but 0 were provided
./HuffmanBase.hpp:11:3: note: candidate constructor not viable: requires 2
arguments, but 0 were provided
HuffmanNode(char c, size_t f) : HuffmanNode(c, f, nullptr, nullptr, nu...
^
./HuffmanBase.hpp:10:3: note: candidate constructor not viable: requires 5
arguments, but 0 were provided
HuffmanNode(char c, size_t f, HuffmanNode *p, HuffmanNode *l, HuffmanN...
^
1 error generated.
答案 0 :(得分:0)
从错误消息的底部开始,编译器正在尝试执行HuffmanNode{}
,但是由于HuffmanNode
没有默认的构造函数而失败。此处的密钥为but 0 were provided
。
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit copy
constructor) not viable: requires 1 argument, but 0 were provided
class HuffmanNode {
^
./HuffmanBase.hpp:8:7: note: candidate constructor (the implicit move
constructor) not viable: requires 1 argument, but 0 were provided
./HuffmanBase.hpp:11:3: note: candidate constructor not viable: requires 2
arguments, but 0 were provided
HuffmanNode(char c, size_t f) : HuffmanNode(c, f, nullptr, nullptr, nu...
^
./HuffmanBase.hpp:10:3: note: candidate constructor not viable: requires 5
arguments, but 0 were provided
HuffmanNode(char c, size_t f, HuffmanNode *p, HuffmanNode *l, HuffmanN...
^
HeapQueue
的默认构造函数正在调用VectorCompleteTree<HuffmanNode>
的默认构造函数。
./HeapQueue.hpp:46:7: note: in instantiation of member function
'VectorCompleteTree<HuffmanNode>::VectorCompleteTree' requested here
class HeapQueue
VectorCompleteTree<HuffmanNode>
的默认构造函数是使用一个元素构造一个std::vector<HuffmanNode>
。此元素需要默认构造。
./HeapQueue.hpp:25:26: note: in instantiation of member function
'std::__1::vector<HuffmanNode, std::__1::allocator<HuffmanNode> >::vector'
requested here
VectorCompleteTree() : V(1) {}
std::vector<HuffmanNode>
试图默认构造HuffmanNode
,但不能。
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1826:31: error:
no matching constructor for initialization of 'HuffmanNode'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1718:18: note:
in instantiation of function template specialization
'std::__1::allocator<HuffmanNode>::construct<HuffmanNode>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1561:14: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::__construct<HuffmanNode>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1030:25: note:
in instantiation of function template specialization
'std::__1::allocator_traits<std::__1::allocator<HuffmanNode>
>::construct<HuffmanNode>' requested here
__alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_));
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/vector:1121:9: note:
in instantiation of member function 'std::__1::vector<HuffmanNode,
std::__1::allocator<HuffmanNode> >::__construct_at_end' requested here
__construct_at_end(__n);
所有错误消息都在那里!简单的解决方案是为HuffmanNode
提供默认的构造函数。