我遇到了一些错误。我需要使用运算符 = 将成员传递给 const 成员。 我不知道错误是什么,我的函数声明是:
template<class I> forceinline
void OverweightValues<I>::init(int t,
SharedArray<int>& elements0,
SharedArray<int>& weights0,
I& i) {
{..}
//both are declared as 'const SharedArray<int> elements, weights;'
//but elements0 and weights0 not is const
elements = elements0;
weights = weights0;
编译时报错:
In line(16): no operator "=" matches these operands operand types are: const SharedArray<int> = SharedArray<int>
In line(17): no operator "=" matches these operands operand types are: const SharedArray<int> = SharedArray<int>
我该如何解决这个问题?
最佳答案
const
成员只能在创建时设置。您需要使用构造函数初始化列表而不是两阶段构造。 (即使是构造函数体也不能赋值)
关于c++ - &member 到 const 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29312670/