0

I Have a problem with creating a copyconstructor for my doublependulum class. I Created one in my doubelpendulum.h file, but it doesn't seem to recognize it in doublependulum.cpp. I keep getting this error :

error C2512: 'Pendulum' : no appropriate default constructor available

I don't understand why I have to add an appropriate constructor or why the one I defined ( rule 103-104) is not correct. If so, could anyone please tell me why it is neccessarry or what is wrong with mine?

pendulum.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

pendulum.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;
}

doublependulum.h

#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

doublependulum.cpp

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

Main.cpp

#include "pendulum.h"
#include "doublependulum.h"
int main()
{return 0;}

Everything compiles until I add rules : 143-147 I get this error:

error C2512: 'Pendulum' : no appropriate default constructor available

error C2512: 'Pendulum' : no appropriate default constructor available

And a worrisome warning

Generating Code... c:\users\niels\documents\visual studio 2010\projects\examenopdracht\examenopdracht\pendulum.cpp(55): warning C4717: 'Pendulum::Pendulum' : recursive on all control paths, function will cause runtime stack overflow

I've been searching for hours and hours and hope someone can explain to me what is wrong with my constructor.

3
  • Excuse me, wrong link : pastebin.com/B4UdXw53 May 8, 2012 at 17:12
  • Please post simplified code in the question which exemplifies the problem. The error you are receiving means you are default initializing a Pendulum somewhere when it has no default constructor defined.
    – AJG85
    May 8, 2012 at 17:20
  • Please do not use pastebin links. When that link goes away, your question becomes useless to future visitors. As AJG85 says, please include simplified code to demonstrate the issue. I've inserted the contents of the link, but please edit it down to a representative, minimal, sample.
    – RivieraKid
    May 9, 2012 at 9:20

4 Answers 4

3

The first problem is that you are attempting to default-construct members PendUp and PendDown of type Pendulum in your class DoublePendulum, while your class Pendulum has no default constructor. You can's default-construct a class that has no default constructor. Either provide a default-constructor for class Pendulum or stop trying to default-construct it in DoublePendulum. Which is the proper solution depends on your intent. Only you know what your intent was.

As it has been noted in other answers, the reason your class Pendulum has no default constructor is that in C++ once you provide any user-defined constructor for the class, the compiler immediately stops providing the implicit default constructor for that class. In your case you explicitly declared two constructors in class Pendulum, which automatically disabled its default constructor. To re-enable it you have to declare it explicitly.


The second problem is rooted in the design of copy-constructor and copy-assignment operator in class Pendulum. You implemented the copy-constructor as a simple call to the copy-assignment operator. However, your copy-assignment operator accepts its parameter by value. This means that in order to prepare the parameter the actual argument has to be copy-constructed by a call to copy-constructor. This is your infinite recursion. I.e. in order to complete the copy-constructor you have to call the copy-assignment, but in order to call the copy-assignment you have to call the copy-constructor.

Firstly, why have you made your copy-assignment in class Pendulum to accept its argument by value? In fact, the current implementation of copy-assignment operator doesn't seem to make any sense at all: it doesn't assign anything to its left-hand side. Make your copy-assignment accept its argument by const reference and the infinite recursion problem should go away. Also, make your copy-assignment to actually assign things to its left-hand side (i.e. the *this object).

Secondly, the very idea of implementing the copy-constructor through a call to copy-assignment operator is broken. Assignment expects an object in a fully-constructed state, while inside the constructor the object is not constructed yet. A better idea would be to do it the other way around: to implement the copy-assignment through copy-construction by using the well-known copy-and-swap idiom.

3
  • Hello, first of all, thanks alot for the clarification. A few months ago i didn't ever heard of c++, so my knowledge is rather small. But as far as I've understood what you are saying, I did the following things: It was confusing to just call to the copy-assignment operator in the copy constructor so it resulted in an infinite recursion. As a solution to this problem I did the following: I Changed my copy constructor in my pendulum class to Pendulum::Pendulum(const Pendulum& Object) : M_(Object.M_), L_(Object.L_), Theta_(Object.Theta_), Omega_(Object.Omega_) {} May 8, 2012 at 19:16
  • @ComputerSaysNo: Great. That will make your copy constructor work and that will eliminate the infinite recursion. However, that does not fix the copy-assignment operator problem. Your copy assignment operator in its original form does not assign anything to its left-hand side. Why? Was that your intent? May 8, 2012 at 19:27
  • I think I found a way to solve that. However, I have some doubt with my use of the "this pointer" 'Pendulum& Pendulum::operator=( const Pendulum & Pendulum2) { (*this).Theta_=Pendulum2.Theta_; (*this).Omega_=Pendulum2.Omega_; return *this; }' Now I really assigned something on the left side. Every Omega_ and Theta_ will be replaced. And M_ and L_ will be the same. That is what I want. May 8, 2012 at 20:57
3

You defined a constructor Pendulum::Pendulum(const double,const double,double,double) so compiler would not define default constructor.

2

These two functions are mutually recursive:

Pendulum Pendulum::operator=(Pendulum param)
{
  return Pendulum(L_, M_, param.Theta_, param.Omega_);
}
//copy constructor
Pendulum::Pendulum(const Pendulum& Object)
{
  *this=Object;
}

The line *this=Object invokes operator=(Pendulum). But, in order to create the local varaible param, the call invokes the copy constructor. The copy constructor then inokes operator=, etc, etc.

Try:

Pendulum& Pendulum::operator=(const Pendulum& param)
2
  • Pendulum& Pendulum::operator=(const Pendulum& param) would be a better choice so that he can retrn *this here . It will be useful in chaining process of object like a=b=c=d
    – Invictus
    May 8, 2012 at 17:24
  • Thanks alot, I changed it :). I see why it went wrong the way I programmed it. May 8, 2012 at 18:47
0

There are problems with assignment operator and copy-constructor. It seems that your class doesn't need these, so just removing them will fix the problem, and seems the cleanest solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.