Thread: no default constructor error

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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