(C++) using a vector as a wrapper in a stack -
edit: original question answered. relevant issue though, didn't feel deemed making new post. why unable use push() , pop() function calls of stack? here errors:
hctree.cpp:65:16: error: no matching member function call '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> >', method 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> >', method not marked const void push(const value_type& __v) {c.push_back(__v);} ^ hctree.cpp:67:16: error: no matching member function call '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> >', method 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> >', method 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> >', function 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 temp /* traverse top of tree */ while(temp->p != null) { /* record path take parent stack */ if(temp == temp->p->c0)//if temp c0 child encoding.push(0); else//temp c1 child encoding.push(1); temp = temp->p;//move temp's parent , repeat } /* write bits buffer */ out.writebit(encoding.pop()); } relevant line hctree.hpp:
stack<int,std::vector<int>> encoding; is there using vector preventing me using push() , pop() function calls?
----original post----: trying create stack stores ints in c++ using vector wrapper, done in example seen here: http://www.cplusplus.com/reference/stack/stack/stack/
i need stack stores ints, have code:
std::vector<int> wrapper; stack<int,std::vector<int>> encoding (wrapper); and following error:
compiling: compress.cpp -> build/compress.o in file included compress.cpp:19: ./hctree.hpp:35:43: error: unknown type name 'wrapper' stack<int,std::vector<int>> encoding (wrapper); how can fix implementation? need create empty stack push 1's , 0's onto backtrace binary tree in order later pop off rebuild path taken.
an empty stack default. need is:
#include <stack> #include <vector> std::stack<int, std::vector<int> > encoding; also, cplusplus.com not source. has more ads , fewer editors. avoid it.
i need create empty stack push 1's , 0's onto backtrace binary tree in order later pop off rebuild path taken.
why not std::stack< bool, std::vector< bool > >? note std::vector< bool > has optimal storage characteristics.
second question
(please not add questions. opening new ones free.)
you cannot change encoding inside member function marked const, because context makes data members behave const. there several solutions:
- let
encodefunction notconst, since meaningfully changes state ofhctreeobject. - do not make
encodingmember ofhctree. put in other object or require supplied user. - declare
encodingmutable. doesn't seem appropriate. state ofmutablemembers should not observable, aside side-effects improved performance.