大家。出于某种原因,我无法使用此声明进行初始化 c ++ priority_queue。例如:
请将滚动条移动到最右边,以便查看我的评论。
class Compare1
{
bool _is_reverse;
public:
Compare1(bool is_reverse = true)
: _is_reverse(is_reverse)
{}
bool operator() (const std::shared_ptr<int> &lhs, const std::shared_ptr<int> &rhs) const
{
return (*lhs > *rhs);
}
};
void test3()
{
Compare1 tester;
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq(Compare1()); // THIS GIVES ME A ERROR
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq(Compare1(false)); // THIS GIVES ME A ERROR TOO
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq1; // THIS IS FINE
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq2(tester); // THIS IS FINE TOO
myq.push(std::make_shared<int>(200));
myq.push(std::make_shared<int>(201));
myq.push(std::make_shared<int>(202));
myq.push(std::make_shared<int>(199));
std::cout << "DEBUG:" << *(myq.top()) << ", " << myq.size() << std::endl;
}
我检查了我的c ++ - stl实现:
#if __cplusplus < 201103L
template<typename _InputIterator>
priority_queue(_InputIterator __first, _InputIterator __last,
const _Compare& __x = _Compare(),
const _Sequence& __s = _Sequence())
: c(__s), comp(__x)
{
__glibcxx_requires_valid_range(__first, __last);
c.insert(c.end(), __first, __last);
std::make_heap(c.begin(), c.end(), comp);
}
#else
template<typename _InputIterator>
**priority_queue(_InputIterator __first, _InputIterator __last,
const _Compare& __x,
const _Sequence& __s)**
: c(__s), comp(__x)
{
__glibcxx_requires_valid_range(__first, __last);
c.insert(c.end(), __first, __last);
std::make_heap(c.begin(), c.end(), comp);
}
template<typename _InputIterator>
priority_queue(_InputIterator __first, _InputIterator __last,
const _Compare& __x = _Compare(),
_Sequence&& __s = _Sequence())
: c(std::move(__s)), comp(__x)
{
__glibcxx_requires_valid_range(__first, __last);
c.insert(c.end(), __first, __last);
std::make_heap(c.begin(), c.end(), comp);
}
#endif
并且,我认为此行有效std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq(Compare1());
但是我收到了那些编译错误消息:
g++ -g -c -std=c++11 main.cc -o main.o
main.cc: In function ‘void test3()’:
main.cc:100:7: error: request for member ‘push’ in ‘myq’, which is of non-class type ‘std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())’
myq.push(std::make_shared<int>(200));
^
main.cc:101:7: error: request for member ‘push’ in ‘myq’, which is of non-class type ‘std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())’
myq.push(std::make_shared<int>(201));
^
main.cc:102:7: error: request for member ‘push’ in ‘myq’, which is of non-class type ‘std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())’
myq.push(std::make_shared<int>(202));
^
main.cc:103:7: error: request for member ‘push’ in ‘myq’, which is of non-class type ‘std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())’
myq.push(std::make_shared<int>(199));
^
main.cc:104:34: error: request for member ‘top’ in ‘myq’, which is of non-class type ‘std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())’
std::cout << "DEBUG:" << *(myq.top()) << ", " << myq.size() << std::endl;
^
main.cc:104:56: error: request for member ‘size’ in ‘myq’, which is of non-class type ‘std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())’
std::cout << "DEBUG:" << *(myq.top()) << ", " << myq.size() << std::endl;
^
make: *** [main.o] Error 1
有人能告诉我这个例子中发生了什么吗?非常感谢。
顺便说一句,我知道std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq(Compare1());
这不是一个好的实践。因为执行此语句后将删除Compare1() object
。 Compare& _x
将引用已删除的对象。
但是,cplusplus.com中的示例表明这有效是出于某些原因。
typedef priority_queue<int,vector<int>,mycomparison> mypq_type;
mypq_type fifth (mycomparison());
mypq_type sixth (mycomparison(true));
我对此非常困惑。
答案 0 :(得分:6)
你一直是C ++ Most Vexing Parse的受害者。
而不是做
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq(Compare1());
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq(Compare1(false));
用括号括起构造函数参数
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq((Compare1()));
// ^ ^
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq((Compare1(false)));
// ^ ^
或使用C ++ 11的统一初始化语法
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq{Compare1()};
// ^ ^
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int>>, Compare1> myq{Compare1(false)};
// ^ ^
现在你如何检查MVP错误?让我们看一个错误消息:
main.cc:100:7: error: request for member ‘push’ in ‘myq’, which is of non-class type ‘std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())’
myq.push(std::make_shared<int>(200));
^
错误消息显示您正在访问非类型 .push()
的{{1}}方法。看看它的类型,特别是这部分
std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())
上述类型是一个函数签名,正如错误消息所指示的那样,它是非类型的。