C – 如何从类的构造函数初始化单独类的构造函数?

前端之家收集整理的这篇文章主要介绍了C – 如何从类的构造函数初始化单独类的构造函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
基本上我想要实现的是在类GeneticIngorithm中创建类deltaKinematics的本地(和私有)实例

在geneticAlgorithm.h文件中,我有:

class DeltaKinematics; //class is defined in separate linked files

class GeneticAlgorithm {
  //private  
    DeltaKinematics deltaRobot;

public:

    GeneticAlgorithm(); //constructor

};

这很好,但是当我去声明GeneticAlgorithm构造函数时,我无法弄清楚如何构造DeltaKinematics的实例

这是geneticAlgorithm.cpp构造函数

GeneticAlgorithm::GeneticAlgorithm(){ //The error given on this line is "constructor for 'GeneticAlgorithm' must explicitly initialize the member 'deltaRobot' which does not have a default constructor"

    DeltaKinematics deltaRobot(100); //this clearly isn't doing the trick

    cout << "Genetic Algorithmic search class initiated \n";
}

如何初始化该本地实例?

解决方法

成员初始化列表:
GeneticAlgorithm::GeneticAlgorithm() : deltaRobot(100) {
}
原文链接:https://www.f2er.com/c/115855.html

猜你在找的C&C++相关文章