CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2005
    Posts
    23

    Inheritance problem (default constructor error)

    hey guys im new here and new to programming in C++
    i have this project for school and ive hit a dead end.

    included i have attached the header, functions cpp, and driver cpp files

    when building i get the following error :error C2512: 'automobile' : no appropriate default constructor available

    now ive read the other thread in here about that same error but, the difference is that i dont have a class within a class, they are inherited classes.

    any help would be extremly appreciated!!!!!!!

    thx guys
    Attached Files Attached Files

  2. #2
    Join Date
    May 2005
    Posts
    47

    Re: Inheritance problem (default constructor error)

    Try to separate arguments in automobile, put them in private and rewrite automobile constructor. I think this may work out for you...


  3. #3
    Join Date
    Jul 2005
    Posts
    23

    Re: Inheritance problem (default constructor error)

    but i need all of the arguments in each other the classes (planes trains and automobiles) to stay in each of the respective constructors, the values come from the base class vehicles. i still dont really understand the nature of the error.

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Inheritance problem (default constructor error)

    You've included a workspace (dsw) file but no project file (dsp). You need to post all your project's files for people to examine it properly.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Inheritance problem (default constructor error)

    Mind you.... one very obvious problem is that in function main() you write:-
    Code:
    	automobile myauto;
    which means you call a constructor with no parameters. However, you don't have any constructor with no parameters. Your only constructor for automobile takes 4 parameters and you must supply these whenever you instantiate an automobile object.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Inheritance problem (default constructor error)

    Oh - and apart from that, you've misunderstood the way that parameters are used in function calles - e.g.

    Code:
    automobile::automobile(double mpg, double fuelcap, int ftype, int tolls) : vehicle(mpg, fuelcap, ftype)
    {
    	tolls = 25;		// This is wrong!!
    	tollfees = tolls;	// This is right
    	mpg = 20;		// This is wrong!!
    	fuelcap = 15;		// and so is this!!
    	ftype = 0;		// and so is this!!
    }
    The basic idea of parameters is that they pass values into your constructor (or your function). Sometimes they can be used to return values out of a function but that wouldn't work with the type of parameter you've set up. What you did in the constructor for vehicle:-
    Code:
    vehicle::vehicle(double mpg, double fuelcap, int ftype)
    {
    	milespergallon = mpg;
    	fuelcapacity = fuelcap;
    	fueltype = ftype;
    
    	// ....
    }
    This is the correct way to use parameters - but what you did in the constructor for automobile would achieve nothing.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    Join Date
    Jul 2005
    Posts
    23

    Re: Inheritance problem (default constructor error)

    thank u so much john e i will edit my code and update with the results. i apreciate the help alot man! im so used to vb which was pretty straight forward with functions and passing arguments.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured