I have got two files and a base class in each of them. I want to let some functions in derived class use data members inside base class. The problem that I am facing resembles closely to the one given below:
In file BaseFile.h
class Base{
private:
int a, b;
protected:
//some data members and functions that I want to share with derived class
public:
Base(int apple, int ball):apple(a), ball(b) {}
};
In file DerivedFile.h
#include "BaseFile.h"
class Derived : public Base{ //error in this line
//stuffs
};
Whenever I declare the derived class, I get an error saying 'no matching function for call to Base::Base note:: candidate expects 2 arguments 0 provided'. What could be the cause of this problem?
First, your initializer list in Base class is wrong. Should be a(apple), b(ball).
Second, you do need to initialize Base class in your Derived class, i.e. call its constructor.
Something like
Derived::Derived() :
Base(0,0) {
}
You have a constructor in the base class that requires two parameters but no default constructor. My guess is that your derived class does not declare constructors so the compiler is trying to create a default for you. It then has no way of calling the base constructor as the only one it has required 2 params.
You are getting this error because Base class doesn't have default constructor (which takes 0 arguments) and from derived class constructor base class default constructor is getting called. This can be fixed in two ways:
Add default constructor to base class
Ensure that Base class constructor is always called as Base(int, int).
Also there is a typo in the Base class constructor code
Base(int apple, int ball):apple(a), ball(b) {}
It should be
Base(int apple, int ball):a(apple), b(ball) {}
Related
I've come across a problem in a class hierarchy I couldn't solve so far. Below you get the minimal example where the template Base class itself inherits from another class that is not a template (called AbsoluteBase in the example below). The class Derived then inherits from Base with its template argument also being Bases' template argument:
#include <iostream>
using namespace std;
class AbsolutBase {
protected:
int number;
AbsoluteBase(int _number) {
number = _number;
}
virtual ~AbsoluteBase() {}
virtual void print() const = 0;
};
template <typename T> class Base : virtual public AbsoluteBase {
public:
Base(int _number) : AbsoluteBase(_number) {}
virtual void print() const {
cout << number << endl;
}
};
template <typename T> class Derived : public Base<T> {
public:
Derived(int _number) : Base<T>::Base(_number) {}
};
int main() {
Derived<char> object(100);
object.print();
}
So each constructor calls the constructor of its parent and passes an integer as argument all the way down to AbsoluteBase. But when compiling the code I get:
error: no matching function for call to 'AbsoluteBase::AbsoluteBase()'
note: candidates are: AbsoluteBase::AbsoluteBase(int)
note: candidate expects 1 argument, 0 provided
Making an instance of Base works just fine but when calling its constructor in the initialization list of Derived, the compiler wants AbsolutBase() as constructor even though the integer argument is given. Obviously, when defining a default constructor in AbsoluteBase the print() function outputs garbage as no value has been passed to number.
So something has to be wrong with my call of Base<T>::Base(int) but I can't see what it is. I am grateful for every explanation and help!
Greetings,
Benniczek
AbsoluteBase is a virtual base class. As such, it must be initialized by the constructor of the most-derived class. Your initializer AbsoluteBase(_number) is valid, but it is only used if you instantiate an object of type Base<T> directly.
The best solution is probably not to make AbsoluteBase a virtual base class.
The reason for this rule is:
class Silly: public Base<int>, Base<long>
{
public:
Silly() : Base<int>::Base(1), Base<long>::Base(2) {}
};
There is only one AbsoluteDerived object (that's what virtual means in this context), so is it initialized with 1 or 2?
Because of virtual inheritance. When base class inherits virtually from another class, the child class of base it must also call constructor of virtual parent of its parent. Since you didn't do this, compiler is trying to call no-argument AbsoluteBase's ctor. So you just have to code as follows:
template <typename T> class Derived : public Base<T> {
public:
Derived(int _number) : AbsoluteBase(_number), Base<T>::Base(_number) {}
};
You declared Absolute Base as class AbsolutBase without the e. Compiles just fine with the typo fixed.
Edit: It also will not compile if you have class base doing virtual inheritance from Absolute. Unless you need virtual inheritance (using multiple inheritance?) you can declare it class Base : public AbsoluteBase
If you do need virtual inheritance (the diamond problem) you'll need to initialize AbsoluteBase in your Derived class. Given that Base virtually inherits from AbsoluteBase, Derived could also inherit from a Base2 which also virtually inherits from AbsoluteBase (this is the point of virtual inheritance, one class can inherit from two different classes that themselves inherit from a common base). As it is a virtual inheritance, there can be only 1 AbsoluteBase even though Base and Base2 could inherit from AbsoluteBase, so how does it get initialized? This is why Derived is required to initialize it directly, so that when the 1 copy of AbsoluteBase is made, it is well understood how it will be initialized.
Again, that is messy, if you don't need virtual inheritance (you probably don't I'd guess...) you can just make Base inherit from AbsoluteBase publically and call it a day.
I have something like this:
Class Base
{
public:
Base();
protected:
someType myObject;
}
Class Child:public someNamespace::Base
{
//constructor
Child(someType x):myObject(x){}
}
Class Child, and Base are in 2 different namespaces...
Compiler complains that my Child class does not have a field called myObject
Anyone know why? Is it because its illegal to populate a Base member from a Child constructor?
Thanks
You cannot initialize the inherited member like that. Instead, you can give the base class a constructor to initialize that member, then the derived class can call that constructor.
class Base
{
public:
Base(someType x) : myObject{x} {}
protected:
someType myObject;
}
class Child : public Base
{
public:
//constructor
Child(someType x) : Base(x) {}
}
In general, a class should be responsible for initializing its own members.
The problem here is that you are initializing your inherited myObject in the initialization list. When an object of the derived class is created, before entering the body of the constructor of the derived class the constructor of the base class is called (by default, if base class has default or no parameter constructor, otherwise you have to explicitly call the constructor in the initialization list).
So, when you do :: Class Child:public someNamespace::Base your constructor for the base class has not yet been called, which is why your compiler complains :: Child class does not have a field called myObject, that is you are actually trying to assign a value to something which has not yet been declared and defined! It will get defined after the constructor the Base class enters its execution.
So, your Child class constructor will look something like this ::
Child(someType x) {
myObject = x;
}
Working ideone link :: http://ideone.com/70kcdC
I found this point missing in the answer above, so I thought it might actually help!
if i run this code
#include<iostream>
using namespace std;
class Final;
class MakeFinal{
public:
friend class Final;
MakeFinal(){cout<<"makefinal\n";}
};
class Final: public virtual MakeFinal{
public:
Final(){cout<<"Final\n";}
};
class Derived:public Final{
public:
Derived(){cout<<"Derived\n";}
};
int main(){
//Final f;
Derived d;
return 0;
}
Output is :
makefinal
Final
Derived
But if i make MakeFinal() constructor private , compiler shows error message. What is this different constructor call hierarchy based on ?
Refer to:
C++ FAQs - virtual inheritance constructors
http://www.parashift.com/c++-faq/virtual-inheritance-ctors.html
Because of the fact that "Initialization list of most-derived-class's ctor directly invokes the virtual base class's ctor. ", your most derived needs to invoke the constructor of the virtual base directly. Therefore, for what you want to do you'd need to make the most derived class a friend too...
Furthermore, it seems that you don't understand virtual inheritance correctly. Refer to this FAQ to understand the purpose and proper use of virtual inheritance.
If your class A have private constructor, you cannot create object a of this class like that (see):
A a;
When an object b of class B that derives from A is created, base class constructor must also be called. If it is private, it cannot be called and the derived object cannot be created.
#include<iostream>
using namespace std;
class base
{
protected:
int a;
public:
base(int i)
{
a=i;
}
};
class derived :protected base
{
public:
derived(){}
void show()
{
cout<<a;
}
};
int main()
{
base obj(2);
derived obj1;
obj1.show();
return 0;
}
Why is this program giving error as In constructor derived::derived():
error: no matching function for call to base::base()
Please explain. As i have read in stackoverflow that in case of inheriting as protected, protected members of base class became protected member of derived class. If I am wrong then please share a good link to clear my misconception
Once you define a constructor for any class, the compiler does not generate the default constructor for that class.
You define the parameterized constructor(base(int i)) for base and hence the compiler does not generate the no argument constructor for base.
Resolution:
You will need to define a constructor taking no arguments for base yourself.
Add this to your base class:
base():a(0)
{
}
EDIT:
Is it needed to define default constructor to every class? Or is it necessary to have the constructor that matches the derived type also present in base type?
The answer to both is NO.
The purpose of constructors in C++ is to initialize the member variables of the class inside the constructor. As you understand the compiler generates the default constructor(constructor which takes no arguments) for every class.
But there is a catch, If you yourself define (any)constructor(one with parameters or without) for your class, the compiler does not generate the default constructor for that anymore. The compiler reasoning here is "Huh, this user writes a constrcutor for his class himself, so probably he needs to do something special in the constructor which I cannot do or understand", armed with this reasoning the compiler just does not generate the default no argument constructor anymore.
Your code above and you assume the presence of the default no argument constructor(When derived class object is created). But since you already defined one constructor for your base class the compiler has applied its reasoning and now it refuses to generate any default argument constructor for your base class. Thus the absence of the no argument constructor in the base class results in the compiler error.
You must give a value to the base constructor:
class Derived : protected base
{
public:
Derived() : base(0)
{
}
};
Depending on your implementation, give the value to the base constructor. However, you may want the Derived constructor to take also int as an argument, and then pass it to the base one.
You need to implement an empty constructor in base or invoke the defined base(int) constructor explicitly from derived c-tor. Without it, when derived c'tor is activated,
it is trying to invoke base() [empty c'tor], and it does not exist, and you get your error.
You haven't defined a default constructor for Base..
When you instantiate an instance of Derived, it will call the Derived() constructor, which will in turn try to call the Base() default constructur which doesn't exist as you haven't defined it.
You can either declare an empty constructor for base Base::Base() or call the existing one as below:
#include<iostream>
using namespace std;
class base
{
protected:
int a;
public:
base(int i)
{
a=i;
}
};
class derived :protected base
{
derived(): Base(123) {} --this will call the Base(int i) constructor)
void show()
{
cout<<a;
}
};
int main()
{
base obj(2);
derived obj1;
obj1.show();
return 0;
}
Lately, I have done much programming in Java. There, you call the class you inherited from with super(). (You all probably know that.)
Now I have a class in C++, which has a default constructor which takes some arguments. Example:
class BaseClass {
public:
BaseClass(char *name); ....
If I inherit the class, it gives me the warning that there is no appropriate default constructor available. So, is there something like super() in C++, or do I have to define a function where I initialize all variables?
You do this in the initializer-list of the constructor of the subclass.
class Foo : public BaseClass {
public:
Foo() : BaseClass("asdf") {}
};
Base-class constructors that take arguments have to be called there before any members are initialized.
In the header file define a base class:
class BaseClass {
public:
BaseClass(params);
};
Then define a derived class as inheriting the BaseClass:
class DerivedClass : public BaseClass {
public:
DerivedClass(params);
};
In the source file define the BaseClass constructor:
BaseClass::BaseClass(params)
{
//Perform BaseClass initialization
}
By default the derived constructor only calls the default base constructor with no parameters; so in this example, the base class constructor is NOT called automatically when the derived constructor is called, but it can be achieved simply by adding the base class constructor syntax after a colon (:). Define a derived constructor that automatically calls its base constructor:
DerivedClass::DerivedClass(params) : BaseClass(params)
{
//This occurs AFTER BaseClass(params) is called first and can
//perform additional initialization for the derived class
}
The BaseClass constructor is called BEFORE the DerivedClass constructor, and the same/different parameters params may be forwarded to the base class if desired. This can be nested for deeper derived classes. The derived constructor must call EXACTLY ONE base constructor. The destructors are AUTOMATICALLY called in the REVERSE order that the constructors were called.
EDIT: There is an exception to this rule if you are inheriting from any virtual classes, typically to achieve multiple inheritance or diamond inheritance. Then you MUST explicitly call the base constructors of all virtual base classes and pass the parameters explicitly, otherwise it will only call their default constructors without any parameters. See: virtual inheritance - skipping constructors
You have to use initiailzers:
class DerivedClass : public BaseClass
{
public:
DerivedClass()
: BaseClass(<insert arguments here>)
{
}
};
This is also how you construct members of your class that don't have constructors (or that you want to initialize). Any members not mentioned will be default initialized. For example:
class DerivedClass : public BaseClass
{
public:
DerivedClass()
: BaseClass(<insert arguments here>)
, nc(<insert arguments here>)
//di will be default initialized.
{
}
private:
NeedsConstructor nc;
CanBeDefaultInit di;
};
The order the members are specified in is irrelevant (though the constructors must come first), but the order that they will be constructed in is in declaration order. So nc will always be constructed before di.
Regarding the alternative to super; you'd in most cases use use the base class either in the initialization list of the derived class, or using the Base::someData syntax when you are doing work elsewhere and the derived class redefines data members.
struct Base
{
Base(char* name) { }
virtual ~Base();
int d;
};
struct Derived : Base
{
Derived() : Base("someString") { }
int d;
void foo() { d = Base::d; }
};
Use the name of the base class in an initializer-list. The initializer-list appears after the constructor signature before the method body and can be used to initialize base classes and members.
class Base
{
public:
Base(char* name)
{
// ...
}
};
class Derived : Base
{
public:
Derived()
: Base("hello")
{
// ...
}
};
Or, a pattern used by some people is to define 'super' or 'base' yourself. Perhaps some of the people who favour this technique are Java developers who are moving to C++.
class Derived : Base
{
public:
typedef Base super;
Derived()
: super("hello")
{
// ...
}
};
There is no super() in C++. You have to call the Base Constructor explicitly by name.