【问题标题】:(C++) unable to call push() and pop() from my stack object(C++) 无法从我的堆栈对象中调用 push() 和 pop()
【发布时间】:2015-04-23 16:06:38
【问题描述】:

为什么我无法使用堆栈的 push() 和 pop() 函数调用?以下是错误:

HCTree.cpp:65:16: error: no matching member function for call to 'push'
  encoding.push(0);
  ~~~~~~~~~^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:197:10: note: 
  candidate function not viable: 'this' argument has type 'const stack<int,
  std::vector<int> >', but method is not marked const
void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:194:10: note: 
  candidate function not viable: 'this' argument has type 'const stack<int,
  std::vector<int> >', but method is not marked const
void push(const value_type& __v) {c.push_back(__v);}
     ^
HCTree.cpp:67:16: error: no matching member function for call to 'push'
  encoding.push(1);
  ~~~~~~~~~^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:197:10: note: 
  candidate function not viable: 'this' argument has type 'const stack<int,
  std::vector<int> >', but method is not marked const
void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:194:10: note: 
  candidate function not viable: 'this' argument has type 'const stack<int,
  std::vector<int> >', but method is not marked const
void push(const value_type& __v) {c.push_back(__v);}
     ^
HCTree.cpp:73:16: error: member function 'pop' not viable: 'this' argument has type 'const
  stack<int, std::vector<int> >', but function is not marked const
  out.writeBit(encoding.pop());
           ^~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:206:10: note: 
  'pop' declared here
void pop() {c.pop_back();}
     ^

代码:

void HCTree::encode(byte symbol, BitOutputStream& out) const
{

  HCNode* temp;
  temp = leaves[symbol];//store leaf node containing symbol into temp
  /* traverse to the top of the tree */
  while(temp->p != NULL)
  {
    /* record path we take to parent into a stack */
    if(temp == temp->p->c0)//if temp is the c0 child
      encoding.push(0);
    else//temp is the c1 child
      encoding.push(1);

    temp = temp->p;//move up to temp's parent and repeat
  }

   /* write bits to buffer */
   out.writeBit(encoding.pop());


}

来自 HCTree.hpp 的相关行:

    stack<int,std::vector<int>> encoding;

使用向量是否会阻止我使用 push() 和 pop() 函数调用?

【问题讨论】:

  • 方法(HCTree::encode)标记为const,不能改变环境变量(方法外的任何东西,标记为非const)。
  • 漂亮的编译器甚至告诉你

标签: c++ vector stack


【解决方案1】:

你用限定符const声明了成员函数

void HCTree::encode(byte symbol, BitOutputStream& out) const
                                                       ^^^^^

这意味着您不能更改对象的数据成员。编译器对此进行了说明。

如果堆栈被声明为可变,您可以更改堆栈。

【讨论】:

  • 哦,有道理,谢谢!不幸的是,这是家庭作业的一部分,我们必须解决给定的函数原型......我有什么选项可以建议您解决这个 const 吗?
  • mutable 是您要查找的关键字
  • @Randy Banks 正如 pm100 已经指出的那样,您可以将堆栈声明为可变的,尽管在我看来这不是一个好主意。我不会在常量 membrt 函数中更改堆栈。
  • 但请注意,家庭作业因此错误。你可以问你的老师这是否是故意的错误。确保您指出了 mutable 修复,以便它显示您无论如何都有解决方案。
  • 也许测试的重点是发现和探索可变的(对于自称是 C++ 专家的人来说,这始终是我的面试问题)
猜你喜欢
  • 2016-08-16
  • 2013-04-20
  • 2013-12-21
  • 2010-09-30
  • 2011-05-04
  • 2023-04-01
  • 1970-01-01
  • 2021-10-14
  • 2021-11-19
相关资源
最近更新 更多