copyconstructor
我在为我的doublependulum
班级创建一个问题时遇到了问题。我在我的doubelpendulum.h
文件中创建了一个,但它似乎无法在doublependulum.cpp
. 我不断收到此错误:
错误 C2512:“Pendulum”:没有合适的默认构造函数可用
我不明白为什么我必须添加适当的构造函数,或者为什么我定义的构造函数(规则 103-104)不正确。如果是这样,谁能告诉我为什么它是必要的或我的有什么问题?
钟摆.h
#include<string>
using std::string;
#ifndef pendulum_H
#define pendulum_H
class Pendulum
{
public:
Pendulum(const double,const double,double,double);
//check function
const double check(const double, const string) const; //used for (L,M)
// getfuncties
double getL() const;
double getM() const;
double getTheta();
double getOmega();
//overload operator
Pendulum operator+ (Pendulum);
Pendulum operator*(const double a);
Pendulum operator=(Pendulum);
//copy constructor
Pendulum(const Pendulum& );
private:
double L_,M_,Theta_,Omega_;
};
#endif
钟摆.cpp
#include "pendulum.h"
#include <iostream>
#include <string>
using namespace::std;
//constructor
Pendulum::Pendulum(const double L, const double M, double Theta, double Omega)
:L_(check(L,"L")),M_(check(M,"M"))
{}
//check functie
const double Pendulum::check(const double d, const string str) const
{
if (d<0.)
{
cout << "ERROR: " << str << " (" << d << ") has to be positive" << endl;
exit(EXIT_FAILURE);
}
return d;
}
//getfuncties
double Pendulum::getM() const
{
return M_;
}
double Pendulum::getL() const
{
return L_;
}
double Pendulum::getTheta()
{
return Theta_;
}
double Pendulum::getOmega()
{
return Omega_;
}
//overloading operators
Pendulum Pendulum::operator+ (Pendulum param)
{
return Pendulum(L_, M_, Theta_+param.Theta_, Omega_+param.Omega_);
}
Pendulum Pendulum:: operator* (const double param)
{
return Pendulum(L_, M_, param*Theta_, param*Omega_);
}
Pendulum Pendulum::operator=(Pendulum param)
{
return Pendulum(L_, M_, param.Theta_, param.Omega_);
}
//copy constructor
Pendulum::Pendulum(const Pendulum& Object)
{
*this=Object;
}
双摆
#ifndef doublependulum_H
#define doublependulum_H
#include "pendulum.h"
class DoublePendulum
{
public:
//constructor
DoublePendulum(Pendulum ,Pendulum );
//getfuncties
Pendulum getUp();
Pendulum getDown();
//Overload operators
DoublePendulum operator+ (DoublePendulum);
DoublePendulum operator*(const double a);
DoublePendulum &operator=(const DoublePendulum &);
//copy constructor
DoublePendulum(const DoublePendulum& );
private:
Pendulum PendUp;
Pendulum PendDown;
};
#endif
双摆
#include "doublependulum.h"
//constructor
DoublePendulum::DoublePendulum(Pendulum Up,Pendulum Down)
:PendUp(Up),PendDown(Down)
{}
//getfunctions
Pendulum DoublePendulum::getUp()
{
return PendUp;
}
Pendulum DoublePendulum::getDown()
{
return PendDown;
}
//Overload operators
DoublePendulum DoublePendulum::operator+ (DoublePendulum param)
{
return DoublePendulum(PendUp + param.PendUp,PendDown + param.PendDown);
}
DoublePendulum DoublePendulum::operator* (const double param)
{
return DoublePendulum(PendUp*param,PendDown*param);
}
DoublePendulum& DoublePendulum::operator= (const DoublePendulum& param)
{
return *this; // assign to members of this object
}
//Copy constructor
DoublePendulum::DoublePendulum(const DoublePendulum& Object)
{
*this=Object;
}
主文件
#include "pendulum.h"
#include "doublependulum.h"
int main()
{return 0;}
一切都会编译,直到我添加规则:143-147 我收到此错误:
error C2512: 'Pendulum' : no appropriate default constructor available
error C2512: 'Pendulum' : no appropriate default constructor available
和一个令人担忧的警告
生成代码... c:\users\niels\documents\visual studio 2010\projects\examenopdracht\examenopdracht\pendulum.cpp(55): 警告 C4717: 'Pendulum::Pendulum' : 在所有控制路径上递归,函数会导致运行时堆栈溢出
我一直在寻找几个小时,希望有人能向我解释我的构造函数出了什么问题。