C++ - 如何从类的构造函数中初始化单独类的构造函数?
基本上我想要实现的目标是在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
成员初始值设定项列表:
Member initializer list:
请注意构造函数名称后面的
:
:它是类的成员数据变量初始化序列的开始。它们显示为对其构造函数的调用,带有您要传递的参数,并且应采用与声明的顺序相同的顺序。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.