Thread: no default constructor error

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663

    no default constructor error

    I get the error:

    error C2512: 'Mouse' : no appropriate default constructor available
    Error executing cl.exe.

    in the following code, and I can't figure out why the class Mouse default constructor is called:
    Code:
    #include <iostream>
    using namespace std;
    
    class Mouse
    {
    public:
         Mouse(int n)
    	 {
    		 length = n;
    	 }	
    	 
    	 void Display()
    	 {
    		 cout<<length<<endl;
    	 }
    
    	 int GetLength()
    	 {
    		 return length;
    	 }
    	 
    private:
         int length;
    };
    
    class Cat
    {
    public:
    	Cat(Mouse m)//********ERROR
    	{
    		member1 = m;
    	}
    	
    
    	void Display()
    	{
    		cout<<member1.GetLength()<<endl;
    	}
    
    private: 
    	Mouse member1; 
    };
    
    int main()
    {
    	Mouse my_Mouse(3);
    
    	Cat my_Cat(my_Mouse);
    
    	my_Cat.Display();
    
    	
        return 0;
    }
    If I add a default constructor to class Mouse, then it works fine. When is the default constructor called?
    Last edited by 7stud; 08-22-2003 at 11:54 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It would be called if you didn't specify any parameters. Like this:
    Mouse my_mouse;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thanks for the reply.

    Anyone else?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    It's because in your Cat class you have as a private member the statement

    Code:
    Mouse member1;
    What do you think this does? Well that would call the default constructor at some point.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    7
    Code:
    Cat(Mouse m):member1(m){}

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Mouse member1;

    What do you think this does? Well that would call the default constructor at some point.


    That's not obvious. This doesn't call the default constructor:

    Mouse a_Mouse(3);
    Mouse m = a_Mouse;

    So, what's the step- by-step process of what's happening(--not at the assembly level). The code doesn't work even if main is empty, and I never try to instantiate an object.
    Last edited by 7stud; 08-23-2003 at 12:37 AM.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by 7stud
    Mouse member1;

    What do you think this does? Well that would call the default constructor at some point.


    That's not obvious. This doesn't call the default constructor:

    Mouse a_Mouse(3);
    Mouse m = a_Mouse;
    You're right that doesn't call the default constructor. That calls the copy constructor. Even though you didn't write your own copy constructor your class is provided with one.

    I wasn't trying to sound like an $$$ in my post. Maybe that's the way it came off, sorry bout that.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    This is a great reason to always use initializer lists for construction. For example, this:

    Code:
    Cat(Mouse m)
    	{
    		member1 = m;
    	}
    does the following:

    1) Constructs its private data member, "member1", via default constructor
    2) Receives a copy (via the copy constructor) of the Mouse passed as the parameter
    3) Uses the assignment operator to equate the member to the copy.

    So you use the default constructor (which is an error), the copy constructor, and the assignment operator.

    In contrast, this:

    Code:
    Cat(Mouse &m) : member1(m)
    	{
    	}
    Does the following:

    1) Constructs its member variable using the copy constructor from the reference (which is passed by address, not by copy).

    So this doesn't call the default constructor OR the assignment operator, which makes it more efficient, and legal (there IS no default constructor).

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thanks, Cat.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 04:21 PM
  5. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM