c++ - error: no viable conversion from '(lambda at A.cpp:21:22)' to 'int'
问题描述
What am I missing? Lambda declaration error. Marked in comment.
void solve()
{
int charCount, time;
cin>> charCount >> time;
// Generating error: no viable conversion from '(lambda at A.cpp:21:22)' to 'int'
int rightDistance = [&](int i)
{
return charCount - i -1;
};
rightDistance(10);
}
解决方案
You're trying to initialize rightDistance
from the lambda itself. You should call the lambda as
int rightDistance = [&](int i)
{
return charCount - i -1;
} (42);
//^^^^
If you want to declare the lambda variable, then declare the type of rightDistance
with auto
instead of int
.
auto rightDistance = [&](int i)
{
return charCount - i -1;
};
rightDistance(10);
推荐阅读
- c - 在 C 中声明字符串的区别
- mysql - 循环中的 Python mysql-connector SELECT 返回过时的结果
- postgresql - 在 PostgreSQL 中存储 ISO 8601 持续时间
- python - 带有 Python 文件的随机库
- python - 我们如何处理标签在机器学习中包含 3 个序数值
- java - 为什么 SynchronizedList 线程的这个包装器是安全的?
- python - “pip install gym”导致“'module' object is not callable”错误
- typescript - ESLint 的更新给出了以下错误:多次导入 import/no-duplicates
- sql - 在电子邮件地址类型为工作的员工表中为电子邮件地址创建一个字段,如果为空则为个人电子邮件
- methods - 方法调用语法 `foo.method()` 和 UFCS `Foo::method(&foo)` 有什么区别?