简体   繁体   English

'非静态引用成员,不能使用默认赋值运算符'

[英]'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.

相关问题 错误:非静态引用成员 - 不能使用默认赋值运算符 - error: non-static reference member - can't use default assignment operator 错误:非静态引用成员,不能使用默认赋值运算符 - Error: non-static reference member, can't use default assignment operator 非静态参考成员&#39;int&Property <int> :: value&#39;,不能使用默认赋值运算符 - non-static reference member ‘int& Property<int>::value’, can’t use default assignment operator 错误:非静态引用成员&#39;std :: ostream&Student :: out&#39;,不能使用默认赋值运算符 - error: non-static reference member 'std::ostream& Student::out', can't use default assignment operator &#39;类型非静态const成员不能使用默认赋值运算符&#39; - 这是什么意思? - 'Type non-static const member can't use default assignment operator' - what does this mean? 非静态 const 成员,不能使用默认赋值运算符 - Non-static const member, can't use default assignment operator 具有非静态lambda成员的类不能使用默认模板参数? - Class with non-static lambda member can't use default template paramers? 为非静态引用成员类编写`operator =` - writing `operator=` for non-static reference member class 默认参数:在非静态成员函数外部无效使用“ this” - default argument : invalid use of 'this' outside of a non-static member function 无法访问游戏对象“必须调用对非静态成员函数的引用” - Can't access game objects “reference to non-static member function must be called”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM