C++ 错误:表达式必须是可修改的左值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22323917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Expression must be a modifiable lvalue
提问by Ben McAlindin
I have been getting this error come up in the for loop when I try to assign values to x_dev, y_dev, and pearson. As far as I can see they should all be modifiable. Can anyone see where I have gone wrong?
当我尝试将值分配给 x_dev、y_dev 和 pearson 时,我一直在 for 循环中出现此错误。据我所知,它们都应该是可修改的。谁能看到我哪里出错了?
class LoopBody
{
double *const x_data;
double *const y_data;
double const x_mean;
double const y_mean;
double x_dev;
double y_dev;
double pearson;
public:
LoopBody(double *x, double *y, double xmean, double ymean, double xdev, double ydev, double pear)
: x_data(x), y_data(y), x_mean(xmean), y_mean(ymean), x_dev(xdev), y_dev(ydev), pearson(pear) {}
void operator() (const blocked_range<size_t> &r) const {
for(size_t i = r.begin(); i != r.end(); i++)
{
double x_temp = x_data[i] - x_mean;
double y_temp = y_data[i] - y_mean;
x_dev += x_temp * x_temp;
y_dev += y_temp * y_temp;
pearson += x_temp * y_temp;
}
}
};
Having followed @Bathsheba 's advice I have overcome these problems. However When running a parallel_for the operator is runs but the for loop is never entered.
按照@Bathsheba 的建议,我已经克服了这些问题。但是,当运行 parallel_for 时,运算符会运行,但永远不会进入 for 循环。
This is where I call the parallel_for:
这是我调用parallel_for的地方:
parallel_for(blocked_range<size_t>(0,n), LoopBody(x, y, x_mean, y_mean, x_dev, y_dev, pearson), auto_partitioner());
回答by Bathsheba
The ()
operator is marked const
, and you're attempting to modify class member data (e.g. x_dev
, y_dev
and person
). That is not allowed and is why you're getting the compile-time error.
该()
操作被标记const
,而你试图修改类的成员数据(例如x_dev
,y_dev
和person
)。这是不允许的,这就是您收到编译时错误的原因。
You probably want to drop the const
from the method.
您可能想const
从方法中删除。
Alternatively you can mark the member data that you want to modify as mutable
, but this is not the preferred solution as it makes code brittle, difficult to read and can wreak havoc with multi-threading.
或者,您可以将要修改的成员数据标记为mutable
,但这不是首选解决方案,因为它使代码变得脆弱、难以阅读并且可能对多线程造成严重破坏。
回答by Alexey Kukanov
Seemingly you want to do reduction, i.e. compute some aggregate values over the data.
似乎您想要进行归约,即计算数据的一些聚合值。
For that, TBB offers a special function template: parallel_reduce
. Unlike parallel_for
that perhaps you use now, parallel_reduce
does not require operator()
of a body class to be const, because an instance of that class accumulates partial results. However, it poses other requirements to the class: the need to have a special constructor as well as a method to merge partial results from another body instance.
为此,TBB 提供了一个特殊的功能模板:parallel_reduce
. 与parallel_for
您现在使用的不同,parallel_reduce
它不需要operator()
body 类为 const,因为该类的实例会累积部分结果。然而,它对类提出了其他要求:需要有一个特殊的构造函数以及一个方法来合并来自另一个主体实例的部分结果。
More information can be found in the Intel(R) TBB User Guide: http://www.threadingbuildingblocks.org/docs/help/tbb_userguide/parallel_reduce.htm
更多信息可在英特尔(R) TBB 用户指南中找到:http: //www.threadingbuildingblocks.org/docs/help/tbb_userguide/parallel_reduce.htm
Also there is an overload of parallel_reduce
which takes two functors - one for body and another one for merging partial results - as well as a special "identity" value used to initialize accumulators. But you are computing three aggregate values at once, so you would still need to have a struct or class to store all three values in a single variable.
还有一个重载parallel_reduce
需要两个函子 - 一个用于主体,另一个用于合并部分结果 - 以及用于初始化累加器的特殊“身份”值。但是您一次计算三个聚合值,因此您仍然需要一个结构或类来将所有三个值存储在一个变量中。