2015-04-23 34 views
1

爲什麼我無法使用堆棧的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()函數調用?

+2

該方法(HCTree :: encode)被標記爲'const',並且不能改變環境變量(方法以外的任何東西,標記爲非''constst'')。 – Caramiriel

+0

好的編譯器甚至告訴你, – pm100

回答

5

你宣佈與預選賽的成員函數const

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

這意味着你可能不會改變對象的數據成員。編譯器說到這一點。

如果聲明爲可變,則可以更改堆棧。

+0

哦,這是有道理的,謝謝!不幸的是,這是家庭作業的一部分,我們必須解決給定的函數原型...我有任何選項可以建議解決這個常量? –

+0

mutable是你正在尋找的關鍵詞 – pm100

+0

@Randy Banks由於pm100已經指出,你可以聲明棧是可變的,儘管在我看來這不是一個好主意。我不會在常量membrt函數中更改堆棧。 –