Thread: No suitable constructor?!?!

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    No suitable constructor?!?!

    Sorry guys, but this is ........ing me off and i've got no clue how to fix it. It's coming up in 4 different codes. Maybe if I get one, the others will make sense.

    Code:
    //Chapter 6 Debug 1
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class Auto
    {
      public:
        int autoId;
        char mechanicName[20];
        double fee;
    	void Auto1(int id, char name[], double amt);
    	//switched above line with Auto1 function, above line initialized variables and added void
        void showAuto();
     };
    void Auto::Auto1(int id = 999, char name[], double amt = 25.00)
     {
       autoId = id;
       strcpy(mechanicName, name);
       fee = amt;
     }
    void Auto::showAuto()
     {
       cout<<"Auto #"<<autoId<<" worked on by "<<mechanicName<<" Amount due $"<<fee<<endl;
     }
    
    void main()
    {
      cout<<"Mike's Service "<<endl<<"Autos Worked on Today"<<endl<<endl;
      cout<<"Mike works on most cars. Occasionally he assigns a job to another mechanic"<<endl;			
      cout<<"Minimum charge $25"<<endl<<endl;
      Auto car1, car2(321), car3(456,"Amy"), car4(567,"Jeremy",149);
      car1.showAuto();
      car2.showAuto();
      car3.showAuto();
      car4.showAuto();
    
      std::cin.clear();
      std::cin.ignore();
      getchar();
    }
    no suitable constructor exists to convert from "int" to "Auto" shows up for the numbers at the bottom: car2(321), car3(456,...), car4(567, ........).

    I'm getting this for an error message:
    1>c:\users\m0531098\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(16): error C2548: 'Auto::Auto1' : missing default parameter for parameter 2
    1>c:\users\m0531098\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(31): error C2440: 'initializing' : cannot convert from 'int' to 'Auto'
    1> No constructor could take the source type, or constructor overload resolution was ambiguous
    1>c:\users\m0531098\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(31): error C2078: too many initializers
    1>c:\users\m0531098\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(31): error C2440: 'initializing' : cannot convert from 'const char [4]' to 'Auto'
    1> No constructor could take the source type, or constructor overload resolution was ambiguous
    1>c:\users\m0531098\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(31): error C2078: too many initializers
    1>c:\users\m0531098\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(31): error C2440: 'initializing' : cannot convert from 'int' to 'Auto'
    1> No constructor could take the source type, or constructor overload resolution was ambiguous

    Help if you can. Thanks.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because there isn't a suitable constructor.

    That constructor would have to be:
    Code:
    Auto(int id = 999, const char* name = "", double amt = 25.00)
    (Also note that you can't have default parameters mixed with arguments that have no default.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. A constructor has the same name as the class. Your class name is Auto, therefore your constructor must also be named Auto, not Auto1.
    2. Class constructor's do not have return types specified for them.
    3. You are still using void main.
    4. You are still using getchar, mixing C/C++ I/O type statements should be avoided, prefer a call to cin.get instead.
    5. The cin.clear/ignore still serve no real purpose in this particular example as in your other post and can be safely removed.
    6. You have nothing in your code from what I see that would require the inclusion of the conio.h header file.


    Quote Originally Posted by anon View Post
    (Also note that you can't have default parameters mixed with arguments that have no default.
    Unless the ones without defaults are all to the left of the ones that do have defaults.

    Code:
    Auto::Auto(int id = 999, char name[], double amt = 25.00);  // Invalid, name (no default) comes after id (has default)
    Auto::Auto(char name[], int id = 999, double amt = 25.00);  // Valid, name (no default) comes before all others with default
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Tried both solutions, now I'm getting:

    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(17): error C2572: 'Auto::Auto' : redefinition of default parameter : parameter 3
    1> c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(12) : see declaration of 'Auto::Auto'
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(17): error C2572: 'Auto::Auto' : redefinition of default parameter : parameter 2
    1> c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(12) : see declaration of 'Auto::Auto'
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2512: 'Auto' : no appropriate default constructor available
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2664: 'Auto::Auto(char [],int,double)' : cannot convert parameter 1 from 'int' to 'char []'
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2664: 'Auto::Auto(char [],int,double)' : cannot convert parameter 1 from 'int' to 'char []'
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2664: 'Auto::Auto(char [],int,double)' : cannot convert parameter 1 from 'int' to 'char []'
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

    Code:
    //Chapter 6 Debug 1
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class Auto
    {
      public:
        int autoId;
        char mechanicName[20];
        double fee;
    	Auto(char name[], int id = 999, double amt = 25.00);
    	//switched above line with Auto1 function, above line initialized variables and added void
        void showAuto();
     };
    Auto::Auto(char name[], int id = 999, double amt = 25.00)  // Valid, name (no default) comes before all others with default 
     {
       autoId = id;
       strcpy(mechanicName, name);
       fee = amt;
     }
    void Auto::showAuto()
     {
       cout<<"Auto #"<<autoId<<" worked on by "<<mechanicName<<" Amount due $"<<fee<<endl;
     }
    
    void main()
    {
      cout<<"Mike's Service "<<endl<<"Autos Worked on Today"<<endl<<endl;
      cout<<"Mike works on most cars. Occasionally he assigns a job to another mechanic"<<endl;			
      cout<<"Minimum charge $25"<<endl<<endl;
      Auto car1, car2(321), car3(456,"Amy"), car4(567,"Jeremy",149);
      car1.showAuto();
      car2.showAuto();
      car3.showAuto();
      car4.showAuto();
    
      std::cin.clear();
      std::cin.ignore();
      getchar();
    }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(17): error C2572: 'Auto::Auto' : redefinition of default parameter : parameter 3
    1> c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(12) : see declaration of 'Auto::Auto'
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(17): error C2572: 'Auto::Auto' : redefinition of default parameter : parameter 2
    1> c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(12) : see declaration of 'Auto::Auto'
    Code:
    class Auto
    {
      public:
        int autoId;
        char mechanicName[20];
        double fee;
        Auto(char name[], int id = 999, double amt = 25.00);
        //switched above line with Auto1 function, above line initialized variables and added void
        void showAuto();
     };
    
    Auto::Auto(char name[], int id = 999, double amt = 25.00)  // Valid, name (no default) comes before all others with default 
     {
       autoId = id;
       strcpy(mechanicName, name);
       fee = amt;
     }
    Drop either the above parts in red or those parts in blue... you choose (does it matter as far as the standard as to where it is preferred you place these?). You should only put those defaults in either the function definition or the class declaration, not both places. Even though you have the same values in both places I'm thinking that maybe the compiler gets confused when you try to assign default values in two separate places.


    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2512: 'Auto' : no appropriate default constructor available
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2664: 'Auto::Auto(char [],int,double)' : cannot convert parameter 1 from 'int' to 'char []'
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2664: 'Auto::Auto(char [],int,double)' : cannot convert parameter 1 from 'int' to 'char []'
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\user\documents\visual studio 2010\projects\debug 6\debug 6\debug 6.cpp(32): error C2664: 'Auto::Auto(char [],int,double)' : cannot convert parameter 1 from 'int' to 'char []'
    1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Code:
    Auto car1, car2(321), car3(456,"Amy"), car4(567,"Jeremy",149);
    Your only constructor has three argument, two of which have defaults. This means that you must provide anywhere from 1 to 3 arguments when constructing an object of class Auto. Notice that this means your first constructor call (for car1) is therefore invalid as there are no arguments being passed into it. This is the reason for the "no appropriate default constructor available" error. It is saying that there is no available constructor you've written that takes 0 arguments.

    Next, notice that your only non-default parameter is the one for the name which is the first argument. This means that all your other constructor calls above are invalid because they require that the name parameter comes first, but you are attempting to pass in a number/integer value instead as the first argument in all those remaining cases. This is what all those "cannot convert parameter 1 from 'int' to 'char []'" errors are saying.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    (does it matter as far as the standard as to where it is preferred you place these?)
    I'm not sure about the standard; I think it doesn't matter. However, it is absolutely horrible style to have default arguments on the definition instead of the declaration.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework help - overloaded constructor taking char or int
    By DHart07 in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2010, 02:57 AM
  2. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  3. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  4. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  5. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM