error: ‘tracker’ is not captured
时间: 2023-12-06 18:04:39 浏览: 40
This error message typically occurs when using a lambda function that attempts to access a variable that is not captured by the lambda.
For example, consider the following code snippet:
```
void foo() {
int tracker = 0;
auto lambda = []() {
std::cout << tracker << std::endl; // error: ‘tracker’ is not captured
};
lambda();
}
```
In this case, the lambda function attempts to access the `tracker` variable, which is declared in the enclosing function `foo()`. However, the `tracker` variable is not captured by the lambda, causing the error message to occur.
To fix this error, you can capture the `tracker` variable by value or reference within the lambda function. For example:
```
void foo() {
int tracker = 0;
auto lambda = [tracker]() { // capture by value
std::cout << tracker << std::endl;
};
lambda();
}
```
or
```
void foo() {
int tracker = 0;
auto lambda = [&tracker]() { // capture by reference
std::cout << tracker << std::endl;
};
lambda();
}
```