为什么不编译?与"运算符&="不匹配
Why isn't this compiling? no match for 'operator&='
当我尝试编译它时,我遇到了一个奇怪的错误:
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:UsersUsuarioDesktopTempC++tc.cpp: In member function `std::string CucumberMarket::check(std::vector<int, std::allocator<int> >, int, int)':
C:UsersUsuarioDesktopTempC++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));
我建议你的重载不要那么接近签名明智。
查看您的错误消息:它正在尝试
ans &= check(..., int, int)
该版本的检查返回一个字符串。 您需要在&=
的右侧使用布尔表达式
相关文章:
- 在使用累加时,C++中的运算符+不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- 不显示匹配运算符
- 映射迭代器与运算符不匹配
- 与"运算符<<"C++不匹配
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 与标准中的"运算符<<"不匹配
- 运算符不匹配*=
- 不断收到错误" 与"运算符>>不匹配";
- 编译我的 3 个文件时,我收到错误,说"运算符="不匹配
- C++ 模板与运算符<不匹配
- 与"运算符="不匹配
- 代码块编译错误与运算符<<不匹配
- 错误:运算符 [] 不匹配。在比较列表中的 int 和 int 时<int>,
- 解决错误:运算符 [] 不匹配
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与“运算符 []”不匹配
- C++ 设置:不匹配 - 运算符