1

I get

error: no matching function for call to 'Base::Base()'

in code

class Base {
private:
     char *field;
public:
    Base(char *c){
        field = c;
    }
};

class Derived : public Base {
public:
    Derived(char *c){}
};

Error disappears as soon as I add

Base() {}

constructor. Why does the C++ compiler strictly require a Base() constructor with no arguments? What if creating a Base object with no arguments makes no sense?

P.S. For example, I do not have the same error in similar Java code because I would have to add

super("")

as a first statement of body of Derived constructor. And that is really reasonable.

3
  • 4
    "What if creating Base object with no arguments makes no sense?" then your Derived class makes no sense. Commented May 10, 2013 at 16:44
  • "What if creating Base object with no arguments makes no sense?" Well, then don't do that? Commented May 10, 2013 at 16:47
  • 2
    What where you thinking when posting this HTML cluttered mess? Stackoverflow handles the syntax highlighting very well if you give it a chance ... Commented May 10, 2013 at 16:48

7 Answers 7

5

C++ doesn't require a default constructor in the base class -- but as in Java, you need to call the equivalent of Super, otherwise the compiler will attempt to call the default constructor (which you don't have, hence the error):

Derived(char *c) : Base(*c) { }

The bit after the : is called the initializer-list, and is most useful -- not only can you call base class constructors there, but you can initialize member variables (: var(val), var2(val2) syntax) that have non-default constructors.

Sign up to request clarification or add additional context in comments.

2 Comments

Java doesn't require default constructor, too.
@user: Right, ambiguous wording on my part, sorry. I meant, in situations like this, you need to manually call the non-default base constructor, as you would in Java.
5

Why does C++ compiler strongly require Base() constructor with no arguments?

Because Derived(char*) attempts to call Base(), since you don't tell it to call any other constructor, which you can do like this:

 Derived(char *c) : Base('x') {}

or, using the char pointed at by the Derived constructor argument, and making sure not to de-reference a null pointer:

 Derived(char *c) : Base( c ? *c : 'x') {}

1 Comment

Good choice of character constant :-)
3

You have to specify the base initializer:

Derived(char * c)  :  Base('x') { }

1 Comment

Good choice of character constant!
2

Thsi is because

Derived(char *c){}

implicitly calls

Base()

unless you provide which constructor to call explicitly It is equivalent to

Derived(char *c) : Base() {}
                   ^^^^^^

And Base::Base() is not available since you have implemented your own constructor. Otherwise it would have been enabled by default. When you provide it explicitly the code complies. but not doing what you want probably.

Comments

1

Your class Base doesn't have a constructor that takes no arguments. You can either make one that does, or add an initalizer for the existing one:

 Derived(char *c) : Base('a') { ... } 

Comments

1

As soon as you explictly create a constructor for Base, the compiler no longer generates a default ctor for you base. That is the reason why you are getting that error message. For example

class Base{}; //here compiler auto-generates 'Base(){}' for you

class Base2{ 
 Base2(char c){}
}; //here compiler does NOT auto-generate `Base2(){}` for you. 
   //Hence that constructor does not exist and you have to create it yourself

Comments

1

Why does C++ compiler strongly require Base() constructor with no arguments?

It doesn't. It requires derived classes to use one of its constructors; if they don't provide any arguments, then it will try to use the default constructor.

What if creating Base object with no arguments makes no sense?

Then the derived class will have to provide the argument(s) for the constructor it uses:

Derived(char *c) : Base(c) {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.