Randy Banks
Randy Banks

Reputation: 371

(C++) unable to call push() and pop() from my stack object

Why am I unable to use the push() and pop() function calls of my stack? Here are the errors:

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();}
     ^

the code:

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());


}

Relevant line from HCTree.hpp:

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

Is there something about using a vector that is preventing me from using the push() and pop() function calls?

Upvotes: 1

Views: 1538

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

You declared the member function with qualifier const

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

This means that you may not change data members of the object. And the compiler says about this.

You could change the stack if it were declared as mutable.

Upvotes: 5

Related Questions