c++ - 为什么这不编译?不匹配 'operator&='

标签 c++

当我尝试编译这个时我遇到了一个奇怪的错误:

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));

我不明白,为什么我会得到这个?我以前做过这个,它总是编译

最佳答案

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));

我建议您的重载在签名方面不要靠得太近。

关于c++ - 为什么这不编译?不匹配 'operator&=',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694985/

相关文章:

c++ - 在 C++ 中调用构造函数

c++ - 错误 : not all control paths return a value

c++ - 由于存在默认成员初始化程序,因此类类型很简单

c++ - 如果消息被 TCP 分段,接收方会做什么

python - 如何在 OpenCV 中计算焦距?

c++ - LibAV FFmpeg C++ av_find_input_format ("dshow") 返回 nullptr

c++ - 使用使用私有(private)字段的基类方法

C++ 用户输入限制

c++ - 没有参数的函数c++

c++ - QWebEnginePage中的setFeaturePermission成员函数是如何工作的?