爲什麼我無法使用堆棧的push()和pop()函數調用?下面是錯誤的:(C++)無法從堆棧對象中調用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();}
^
代碼:從HCTree.hpp
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());
}
相關線路:
stack<int,std::vector<int>> encoding;
有一些關於使用阻止我使用的矢量push()和pop()函數調用?
該方法(HCTree :: encode)被標記爲'const',並且不能改變環境變量(方法以外的任何東西,標記爲非''constst'')。 – Caramiriel
好的編譯器甚至告訴你, – pm100