![](/img/trans.png)
[英]error: non-static reference member - can't use default assignment operator
[英]'non-static reference member, can't use default assignment operator'
当我尝试编译我的代码时出现此错误: non-static reference member 'Timestep& Timestep::previousTimestep', can't use default assignment operator
我创建了一个Problem
,它创建了一个Timestep
引用,这个Timestep
应该存储在vector solution
。 此外,我想存储对前一个Timestep
的引用 - 以及第一个将自己引用的Timestep ......
我读到我需要定义一个自己的运算符,如果我在类中有const
成员我尝试设置相等。 但是,删除所有const
元素形成代码,它仍然无法正常工作。 有什么建议? 非常感谢。
class Problem {
public:
void initialTimestep(arma::vec ic);
private:
std::vector<Timestep> solution;
};
void Problem::initialTimestep(vec ic){
Timestep myFirstTimestep(starttime, ic, nodes);
solution.push_back(myFirstTimestep);
}
class Timestep {
public:
Timestep(double starttime, arma::vec initialCondition, arma::vec nodelist);
private:
Timestep& previousTimestep; //const
};
Timestep::Timestep(double starttime, vec initialCondition, vec nodelist)
: previousTimestep(*this)
{
//do stuff
}
int main() {
int k = 3; //subdomains
vec v = linspace(0., 1., k+1); //node spacing
vec ic= ones<vec>(k+1); //initialconditions
Problem myProblem(v, ic, 0., 1., 0.1);
return 0;
}
没有为您的类Timestep
创建默认赋值运算符,因为它包含一个引用(以后不能设置它。它基本上是指向非const数据的常量指针)。 solution.push_back(myFirstTimestep)
虽然需要asignment(或者用c ++ 11移动),所以你必须定义自己的赋值(或移动)操作符(当然除非你将Timestep& previousTimestep
更改为to,否则你将无法执行Timestep& previousTimestep
操作。 Timestep *previousTimestep
在这种情况下,默认赋值也可以正常工作)。
您需要为Timestep类( operator=
)编写自己的赋值运算operator=
。
或者,您可以在Timestep类中使用Timestep指针而不是引用。 在这种情况下,这是我个人的偏好。 由于各种原因,编译器对指针的规则要少得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.