basic constructor with vector : Inappropriate default constructor - c++

Here is the code to demonstrate the classic problem
class A
{
public:
A(int){}
};
class B
{
vector<A> va; //Error no default constructor available
public:
B(vector<A>v):va(v)
{}
};
The error is no default constructor available. I don't need default constructor for class A, so don't write it.

Adding a trivial constructor for A will work for you
class A
{
public:
A(int) {}
A() {}
};

Related

C++ Cannot construct class that extends template [duplicate]

Why does this code:
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
};
int main(void)
{
B *b = new B(5);
delete b;
}
Result in these errors:
main.cpp: In function ‘int main()’:
main.cpp:13: error: no matching function for call to ‘B::B(int)’
main.cpp:8: note: candidates are: B::B()
main.cpp:8: note: B::B(const B&)
Shouldn't B inherit A's constructor?
(this is using gcc)
If your compiler supports C++11 standard, there is a constructor inheritance using using (pun intended). For more see Wikipedia C++11 article. You write:
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
using A::A;
};
This is all or nothing - you cannot inherit only some constructors, if you write this, you inherit all of them. To inherit only selected ones you need to write the individual constructors manually and call the base constructor as needed from them.
Historically constructors could not be inherited in the C++03 standard. You needed to inherit them manually one by one by calling base implementation on your own.
For templated base classes, refer to this example:
using std::vector;
template<class T>
class my_vector : public vector<T> {
public:
using vector<T>::vector; ///Takes all vector's constructors
/* */
};
Constructors are not inherited. They are called implicitly or explicitly by the child constructor.
The compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). But if you want a constructor that will accept an int, you have to define it explicitly.
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
public:
explicit B(int x) : A(x) { }
};
UPDATE: In C++11, constructors can be inherited. See Suma's answer for details.
This is straight from Bjarne Stroustrup's page:
If you so choose, you can still shoot yourself in the foot by inheriting constructors in a derived class in which you define new member variables needing initialization:
struct B1 {
B1(int) { }
};
struct D1 : B1 {
using B1::B1; // implicitly declares D1(int)
int x;
};
void test()
{
D1 d(6); // Oops: d.x is not initialized
D1 e; // error: D1 has no default constructor
}
note that using another great C++11 feature (member initialization):
int x = 77;
instead of
int x;
would solve the issue
You have to explicitly define the constructor in B and explicitly call the constructor for the parent.
B(int x) : A(x) { }
or
B() : A(5) { }
How about using a template function to bind all constructors?
template <class... T> Derived(T... t) : Base(t...) {}
Correct Code is
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
public:
B(int a):A(a){
}
};
main()
{
B *b = new B(5);
delete b;
}
Error is b/c Class B has not parameter constructor and second it should have base class initializer to call the constructor of Base Class parameter constructor
Here is how I make the derived classes "inherit" all the parent's constructors. I find this is the most straightforward way, since it simply passes all the arguments to the constructor of the parent class.
class Derived : public Parent {
public:
template <typename... Args>
Derived(Args&&... args) : Parent(std::forward<Args>(args)...)
{
}
};
Or if you would like to have a nice macro:
#define PARENT_CONSTRUCTOR(DERIVED, PARENT) \
template<typename... Args> \
DERIVED(Args&&... args) : PARENT(std::forward<Args>(args)...)
class Derived : public Parent
{
public:
PARENT_CONSTRUCTOR(Derived, Parent)
{
}
};
derived class inherits all the members(fields and methods) of the base class, but derived class cannot inherit the constructor of the base class because the constructors are not the members of the class. Instead of inheriting the constructors by the derived class, it only allowed to invoke the constructor of the base class
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
B(int x):A(x);
};
int main(void)
{
B *b = new B(5);
delete b;
}

Inheritance without default constructor?

Since the inherited class has no default constructor, I explicitly call the inherited class's constructor, and I still got the red-underline(in VScode) at B's constructor:
no default constructor exists for class "A"
Does class A have to obtain default constructor? Is there any way to fix this?
This is simplified code:
class A
{
public:
int a_;
A(int a): a_(a)
{}
};
class B: public A
{
public:
A A1;
A A2;
B(int a1, int a2): A1(a1), A2(a2)
{}
};
The error is about A not having a default constructor. Because it doesn't, there's no way to initialize the A part of B. You need something like:
B(int a, int a1, int a2): A(a), A1(a1), A2(a2)
{}

Copy constructor calling copy constructor of another class

I was wondering, can I and if I do, how to call a copy constructor of template class A in copy constructor of class B, both class are not in the same file.
template<typename T> class A: public AP<T>
{
public:
A();
A(const A&);
~A();
};
#include "A"
class B: public BP
{
private:
A<int> Amember;
public:
B();
B(const B&);
~B();
};
Thanks :)
If I understand the question, I think you're after the optional initializer list in the constructor of your B class. Doesn't matter if you want to call the default constructor or copy constructor. In the definition of the constructor, you can put a colon and the list of how each member variable should be initialized. i.e., which constructor should be called.
#include <iostream>
using namespace std;
class A{
public:
A(){cout<<__PRETTY_FUNCTION__<<endl;}
A(int i){cout<<__PRETTY_FUNCTION__<<" "<<i<<endl; data = i;}
A(const A& a){cout<<__PRETTY_FUNCTION__<<endl; data = a.data;}
private:
int data;
};
class B{
public:
B() : a(){}
B(int i) : a(i){}
B(const B& b) : a(b.a) {}
private:
A a;
};
int main(){
B b1; // default constructor
B b2(5); // integer constructor
B b3(b2); // copy constructor
}

Simple copy constructor in C++

Let's say we have two classes in C++:
Class A{
public:
A();
private:
int k;
};
Class B{
public:
B();
private:
A a;
};
I edit my question such that it is more helpful for anyone does reach it someday.
How could I write the copy ctor of B (is it a copy ctor indeed?) for initializing a (which is of type Class A) with another object instance of A (let it be a_inst) which have already been defined and initialized before?
In other words, what would be the code for the ctor B()?
I would do it like this
B(const A &paramA) : a(paramA) {}
B(A &&paramA) : a(move(paramA)) {}
B(const B &src) : a(src.a) {}
Beside the copy constructor I am also proposing two additional constructors, one that can be initialized by l-value instances of A and other for r-value instances. Depending on the semantics of A you may not need the move version.
Class A{
public:
A();
A(const A& obj);
private:
int k;
};
Class B{
public:
B();
B(const B& obj)
: a(obj.a) { }
B(const A& obj)
: a(obj) { }
private:
A a;
};

Is it valid to copy an inherited member in the derived class constructor?

In the below code I have defined an explicit copy constructor in the derived class. I have also written my own copy constructor in the base class.
Primer says that the derived copy constructor must call the base one explicitly and that inherited members should be copied by the base class copy constructor only, but I copied the inherited members in the derived class copy constructor and it is working fine. How is this possible?
Also how do I explicitly call the base class copy constructor inside the derived class copy constructor? Primer says base(object), but I am confused how the syntax differentiates between normal constructor call and copy constructor call.
Thanks in advance.
#include<stdafx.h>
#include<iostream>
using namespace std;
class A
{
public:
int a;
A()
{
a = 7;
}
A(int m): a(m)
{
}
};
class B : public A
{
public:
int b;
B()
{
b = 9;
}
B(int m, int n): A(m), b(n)
{
}
B(B& x)
{
a = x.a;
b = x.b;
}
void show()
{
cout << a << "\t" << b << endl;
}
};
int main()
{
B x;
x = B(50, 100);
B y(x);
y.show();
return 0;
}
copy constructor is when you have another Object passed to your constructor:
class A() {
private:
int a;
public:
//this is an empty constructor (or default constructor)
A() : a(0) {};
//this is a constructor with parameters
A(const int& anotherInt) : a(anotherInt) {};
//this is a copy constructor
A(const A& anotherObj) : a(anotherObj.a) {};
}
for a derived class
class B : public A {
private:
int b;
public:
//this is the default constructor
B() : A(), b() {};
//equivalent to this one
B() {};
//this is a constructor with parameters
// note that A is initialized through the class A.
B(const int& pa, const int& pb) : A(pa), b(pb) {}
//for the copy constructor
B(const B& ob) : A(ob), b(ob.b) {}
}
How somebody else wrote here:
How to call base class copy constructor from a derived class copy constructor?
I would write the copy constructor like this instead how it was written by macmac:
B(const B& x) : A(x) , b(x.b)
{
}
To call the copy constructor of the base A you simply call it passing the derived B object A(B), is not neccessary to specify B.a.
EDIT: macmac edited his answere in the correct way, now his answere is better then mine.