1

每个人。由于某种原因,我无法使用此语句来初始化 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一旦执行此语句就会被删除。aCompare& _x将引用已删除的对象。

但是,cplusplus.com中的示例表明由于某些原因这是有效的。

typedef priority_queue<int,vector<int>,mycomparison> mypq_type;
mypq_type fifth (mycomparison());
mypq_type sixth (mycomparison(true)); 

在这一点上我很困惑。

4

1 回答 1

6

您一直是 C++最令人烦恼的 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()的方法。看它的类型,特别是这部分 std::priority_queue<std::shared_ptr<int>, std::vector<std::shared_ptr<int> >, Compare1>(Compare1 (*)())

(Compare1 (*)())

上述类型是一个函数签名,正如错误消息所表明的那样,它是非类类型。

于 2013-07-29T02:16:07.123 回答