Thread: Erros with lack of default constructors and inheritance

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    Erros with lack of default constructors and inheritance

    We're dealing with inheritance and we've been asked to remove the default constructors. I've been looking for answers in 3 different books and online and how found nothing really helpful.

    GameEvent.cpp: In constructor ‘GameEvent::GameEvent(Time)’:
    GameEvent.cpp:14: error: type ‘Time’ is not a direct base of ‘GameEvent’
    GameEvent.cpp:14: error: no matching function for call to ‘Time::Time()’
    Time.h:20: note: candidates are: Time::Time(int, int, double)
    Time.h:13: note: Time::Time(const Time&)
    GameEvent.h
    Code:
     
    #ifndef GAMEEVENT_H
    #define GAMEEVENT_H
    
    #include <string>
    
    using namespace std;
    
    #include "Time.h"
    #include "Game.h"
    
    class GameEvent {
    protected:
    	Time time;
    public:
    	//	GameEvent();
    	GameEvent(const Time t);
    	Time getTime();
    	const virtual string toString() = 0;
    };
    
    #endif /*GAMEEVENT_H*/

    Time.cpp
    Code:
     #ifndef TIME_H
    #define TIME_H
    
    #include <string>
    
    using namespace std;
    
    class Time {
    private:
    	int period;
    	double seconds;
    
    public:
    	/* Time(); */
    	Time(int p, int m, double s);
    	
    	double difference(const Time& other) const;
    	
    	bool operator<(Time& other);
    	
    	const string toString();
    	
    	bool operator==(Time& other);
    	static const int NORMAL_SECONDS = 60*15; //Fifteen minutes
    	static const int OVERTIME_SECONDS = 60*5; //Five minutes
    };
    
    ostream& operator<<(ostream& out, Time& other);
    
    #endif /*TIME_H*/

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    well, default constructors are constructors that YOU do not write, i.e. the compiler writes them for you.

    So, to remove default constructors, you must write your own. hope that helps

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    what we had was something like:

    Code:
    Time(){} and GameEvent(){}
    We were asked to remove these, and once we did we started getting those errors.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, your base class has a non-trivial constructor (a constructor that takes more one argument or more), so the compiler cannot know which constructor to call or which arguments to pass in.
    Here's where you come in. You need to tell the compiler by adding ": ConstructorName(args)" after the constructors in your derived class. Basically it tells the compiler which constructor in the base class to call and what arugments to pass.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by smasherprog
    well, default constructors are constructors that YOU do not write, i.e. the compiler writes them for you.
    This is not true. Default constructors are constructors that can be invoked with no arguments. What you are referring to are implicitly declared default constructors.

    Quote Originally Posted by Elysia
    Yes, your base class has a non-trivial constructor (a constructor that takes more one argument or more), so the compiler cannot know which constructor to call or which arguments to pass in.
    To elaborate: by default, a constructor for a class will invoke the default constructors of the non-static member variables and base classes. However, the Time class has a non-default constructor, thus it does not have an implicitly declared default constructor. As such, you either have to declare the default constructor, or since you were told not to do so, you have to use the constructor initialisation list as Elysia described.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance and constructors
    By coletek in forum C++ Programming
    Replies: 6
    Last Post: 07-04-2009, 01:35 AM
  2. How constructors are called in c++ (inheritance)
    By Spirytus in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2007, 06:18 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. constructors and inheritance
    By paperbox005 in forum C++ Programming
    Replies: 12
    Last Post: 08-16-2004, 02:46 AM

Tags for this Thread