C++ 为什么这不是我的?“运算符&=”不匹配
当我试图编译此文件时,遇到了一个奇怪的错误:C++ 为什么这不是我的?“运算符&=”不匹配,c++,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;
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它似乎是在假设check返回string的情况下。这是你的问题,你希望它采取布尔返回一个。
若你们想让它正常工作,最简单的解决办法就是强制演员价格[i]的长度如下
forint i=0;i我建议您的重载不要靠得太近。请看您的错误消息:它正在尝试
ans &= check(..., int, int)
该版本的check返回一个字符串。在&=的右侧需要一个布尔表达式,我打赌它调用第二个重载,它返回一个字符串。