Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:34:28+00:00 2026-06-03T13:34:28+00:00

I Have a problem with creating a copyconstructor for my doublependulum class. I Created

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:34:28+00:00Added an answer on June 3, 2026 at 1:34 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem creating and executing a JAR file. I have already made
Why I have problem creating a class inheriting from str (or also from int)
I have a problem creating a DLL file from a JAR file with IKVM
I have a problem in creating subqueries with Hibernate. Unfortunately the Subqueries class is
have a problem creating my new table in SqlLite3 I have created this migration
I have problem in creating a separate function to read file in Hadoop Streaming.
I have problem when creating a file in encoding 'utf-8' and reading it from
I have a problem creating a custom dialog. But I don't find the failure.
I have problem with creating list of models. Suppose I've created model: > rp
I have problem creating new instance of excel 2007 using VBA (from Access 2002).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.