[英]'non-static reference member, can't use default assignment operator'
I get this error when i try to compile my code: non-static reference member 'Timestep& Timestep::previousTimestep', can't use default assignment operator
当我尝试编译我的代码时出现此错误:
non-static reference member 'Timestep& Timestep::previousTimestep', can't use default assignment operator
I create one Problem
which creates a Timestep
a reference to the this Timestep
should be stored in the vector solution
. 我创建了一个
Problem
,它创建了一个Timestep
引用,这个Timestep
应该存储在vector solution
。 Besides i want to store a reference to a previous Timestep
- and for the first Timestep that would be a reference to itself... 此外,我想存储对前一个
Timestep
的引用 - 以及第一个将自己引用的Timestep ......
I read that i need to define an own operator if i have const
members in a class what i try to set equal. 我读到我需要定义一个自己的运算符,如果我在类中有
const
成员我尝试设置相等。 However, removed all const
elements form the code and it still don't work. 但是,删除所有
const
元素形成代码,它仍然无法正常工作。 Any suggestions? 有什么建议? Thanks a lot.
非常感谢。
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;
}
No default assignment operator was created for your class Timestep
because it contains a reference (which cannot be set later. it basically is a constant pointer to non-const data). 没有为您的类
Timestep
创建默认赋值运算符,因为它包含一个引用(以后不能设置它。它基本上是指向非const数据的常量指针)。 solution.push_back(myFirstTimestep)
requires the asignment though (or move with c++11) so you will have to define your own assignment (or move) operator (which of course you will not be able to do unless you change Timestep& previousTimestep
to Timestep *previousTimestep
in which case the default assignment will work as well). solution.push_back(myFirstTimestep)
虽然需要asignment(或者用c ++ 11移动),所以你必须定义自己的赋值(或移动)操作符(当然除非你将Timestep& previousTimestep
更改为to,否则你将无法执行Timestep& previousTimestep
操作。 Timestep *previousTimestep
在这种情况下,默认赋值也可以正常工作)。
You need to write your own assignment operator for the Timestep class ( operator=
). 您需要为Timestep类(
operator=
)编写自己的赋值运算operator=
。
Alternatively, you can use a Timestep pointer instead of a reference inside the Timestep class. 或者,您可以在Timestep类中使用Timestep指针而不是引用。 That'd be my personal preference in this case.
在这种情况下,这是我个人的偏好。 The compiler imposes a lot fewer rules about pointers, for various reasons.
由于各种原因,编译器对指针的规则要少得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.