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

发布于 2024-11-07 20:57:08 字数 902 浏览 3 评论 0原文

基本上我想要实现的目标是在genicAlgorithm类中创建deltaKinematics类的本地(私有)实例

genicAlgorithm.h中> 我有的文件:

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

class GeneticAlgorithm {
  //private  
    DeltaKinematics deltaRobot;

public:

    GeneticAlgorithm(); //constructor

};

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

这是genicAlgorithm.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";
}

如何初始化该本地实例?

Basically what I am trying to achieve is to create a local (and private) instance of the class deltaKinematics in the class geneticAlgorithm

In the geneticAlgorithm.h file I have:

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

class GeneticAlgorithm {
  //private  
    DeltaKinematics deltaRobot;

public:

    GeneticAlgorithm(); //constructor

};

This is all fine, but when I go to declare the GeneticAlgorithm constructor, I can't work out how to construct the instance of DeltaKinematics

This is the geneticAlgorithm.cpp constructor:

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";
}

How do I go about initializing that local instance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-11-14 20:57:08

成员初始值设定项列表:

GeneticAlgorithm::GeneticAlgorithm() : deltaRobot(100) {
}

Member initializer list:

GeneticAlgorithm::GeneticAlgorithm() : deltaRobot(100) {
}
深爱不及久伴 2024-11-14 20:57:08
GeneticAlgorithm::GeneticAlgorithm() : deltaRobot(100) {
    cout << "Genetic Algorithmic search class initiated \n";
}

请注意构造函数名称后面的 ::它是类的成员数据变量初始化序列的开始。它们显示为对其构造函数的调用,带有您要传递的参数,并且应采用与声明的顺序相同的顺序。

GeneticAlgorithm::GeneticAlgorithm() : deltaRobot(100) {
    cout << "Genetic Algorithmic search class initiated \n";
}

Note the : after the constructor name: it is the beginning of the initialization sequence for the member data variables of the class. They appear as calls to their constructors, with the parameters you want to pass, and should be in the same order they're declared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文