0

I'm getting a strange error when I try to compile this:

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";
  }
};

This is what I'm getting:

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]

Line 24 is this:

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

I don't get it, why am I getting this? I have done this before and it's always compiled

4

2 に答える 2

7

std::string CucumberMarket::checkcheck が返る前提のようstringです。これはあなたの問題ですbool。戻ってきた問題を受け取ることが期待されています。
正しく動作させたい場合、最も簡単な修正は、以下のように強制的にキャストすること price[i]ですlong long

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

オーバーロードが署名に関してそれほど接近しないことをお勧めします。

于 2012-12-04T02:02:34.397 に答える
2

あなたのエラーメッセージを見てください:それはしようとしています

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

そして、そのバージョンの check は文字列を返します。の右側にブール式が必要です&=

于 2012-12-04T02:06:12.663 に答える