2012-12-04 23 views
0

我得到一個奇怪的錯誤,當我嘗試編譯如下:爲什麼不編譯?敵不過「運營商=」

class CucumberMarket { 
public: 
    bool ans; 
    int n,cont,K,b; 
    bool check(const vector<int> &precios,long long price,int pos) { 
    ++cont; 
    if(cont == K and b < price) ans = false; 
    if(!ans) return ans; 
    for(int i = pos + 1; i < n; ++i) { 
     if(cont < K) ans &= check(precios,price + precios[i],i); 
    } 
    --cont; 
    } 
    string check(vector <int> price, int budget, int k) { 
    n = price.size(); 
    K = k; 
    b = budget; 
    ans = true; 
    cont = 0; 
    for(int i = 0; i < n and ans; ++i) ans &= (this -> check(price,price[i],i)); 
    return ans ? "YES" : "NO"; 
    } 
}; 

這就是我得到:

C:\Users\Usuario\Desktop\Temp\C++\tc.cpp: In member function `std::string CucumberMarket::check(std::vector<int, std::allocator<int> >, int, int)': 
C:\Users\Usuario\Desktop\Temp\C++\tc.cpp:24: error: no match for 'operator&=' in '((CucumberMarket*)this)->CucumberMarket::ans &= CucumberMarket::check(std::vector<int, std::allocator<int> >, int, int)(vector<int,std::allocator<int> >(((const std::vector<int, std::allocator<int> >&)((const std::vector<int, std::allocator<int> >*)(&price)))), (&price)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator<int>](((unsigned int)i)), i)' 
[Finished in 0.2s with exit code 1] 

第24行是這樣的:

for(int i = 0; i < n and ans; ++i) ans &= (this -> check(price,price[i],i)); 

我不明白,爲什麼我得到這個?我以前做過這個,它總是編譯

+2

右邊的布爾表達式我打賭它調用第二個重載,它返回一個字符串。 – chris

回答

7

std::string CucumberMarket::check它似乎是在假設檢查返回string。這是你的問題,你期望它會返回一個bool
如果你想讓它正常工作,最簡單的解決將是迫使投price[i]long long如下

for(int i = 0; i < n and ans; ++i) ans &= (this -> check(price,(long long)price[i],i));

我建議你重載不如此聰明併攏簽名。

+0

謝謝,我剛纔犯的最愚蠢的錯誤。 – hinafu

+0

歡迎您! –

2

看看你的錯誤信息:它試圖

ans &= check(..., int, int) 

,並檢查該版本返回一個字符串。你需要在&=