C ++ - 如何从类的构造函数初始化一个单独的类的构造函数? [英] C++ - How do I initialize a constructor of a separate class from the constructor of a class?
本文介绍了C ++ - 如何从类的构造函数初始化一个单独的类的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我想实现的是在 geneticAlgorithm $类中创建类
deltaKinematics
的本地c $ c>
Basically what I am trying to achieve is to create a local (and private) instance of the class deltaKinematics
in the class geneticAlgorithm
在 geneticAlgorithm.h
文件中,我有:
class DeltaKinematics; //class is defined in separate linked files
class GeneticAlgorithm {
//private
DeltaKinematics deltaRobot;
public:
GeneticAlgorithm(); //constructor
};
这很好,但是当我去声明 GeneticAlgorithm
构造函数,我无法解决如何构建
DeltaKinematics的实例
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
这是 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) {
}
这篇关于C ++ - 如何从类的构造函数初始化一个单独的类的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文