Error while inheriting from one nested class to another - c++

//In file1.hpp
class A
{
protected:
class B
{
public:
B(int a, int b)
: c(a),
d(b) {}
private:
int c;
int d;
};
};
// In file2.hpp
Class C
{
public:
class D : public A::B --------- This line shows the error that "no matching function for call to B"
{
public:
D() {};
virtual ~D() {};
//something
}
class E : public D
{
E() {};
virtual ~E() {};
}
}
I am getting an error while inheriting the A::B class in D class.
In my main.cpp, I instantiate the D class directly. I am assuming that when D class is instantiated, class A::B gets instantiated automatically.
The error is because I am instantiating D class but not giving the parameters for the instantiation of A::B class.
Right now in main.cpp file, I just write
C::D obj;
My question is, how do I instantiate from main.cpp, the class D and also give parameters for class A::B? I do not want to pass as parameters while instantiating class D. Is there a way to create a method?
In short I can create a method in main.cpp to pass parameters for class A::B but not pass parameters like C::D obj(hi, hey)

First you need to declare "class B" as public and you also need to call calls B constructor from class D (or add defalut constructor)
Sample code:
//In file1.hpp
class A
{
public:
class B
{
public:
//B(){}
B(int a, int b)
: c(a),
d(b) {}
private:
int c;
int d;
};
};
// In file2.hpp
class C
{
public:
class D : public A::B //--------- This line shows the error that "no matching function for call to B"
{
public:
D(): B(1,2)
{};
virtual ~D() {};
//something
};
class E : public D
{
E() {};
virtual ~E() {};
};
};
int main()
{
C c;
return 0;
}

Related

How to access variable "a" of "class A" from object of "class D"

I have a Class B, which is inheriting Class A.
I have a Class C, which is inheriting Class B and Class A;
There is a variable named "a" in class A which I have to access in class D, but not by using resolution operator on class B for example , d.B::a;
#include<bits/stdc++.h>
using namespace std;
class A{
public:
int a;
};
class B : public A{
public:
int b;
};
class D : public B, public A{
public:
int d;
};
int main() {
D d;
d.B::a = 1; // This is working correctly on this path class D -\> class B -\> class A
d.a = 2;
/*
But This line gives the following error
Non-static member 'a' found in multiple base-class subobjects of type 'A':
class D -> class B -> class A
class D -> class A
*/
return 0;
}
int main() {
D d;
d.D::a = 2; //This is not working correctly on this path class D -\> class A
d.A::a = 2; //This is not working correctly on this path class D -\> class A
//How do I use Scope Resolution to access variable "a" from second path ?!
return 0;
}
B already inherits from A, so D should only inherit from B:
class A{
public:
int a;
};
class B : public A{
public:
int b;
};
class D : public B{
public:
int d;
};
If you had another class C that also inherits from A, and you would want D to inherit from B and C, then you would have the diamond problem. Then, to avoid ambiguity, you should write:
class A{
public:
int a;
};
class B : virtual public A{
public:
int b;
};
class C : virtual public A{
public:
int b;
};
class D : public B, public C{
public:
int d;
};
Virtual inheritance is key words for your problem.
Try this one :
#include<bits/stdc++.h>
using namespace std;
class A{
public:
int a;
};
class B : virtual public A{
public:
int b;
};
class D : public B, virtual public A{
public:
int d;
};
int main() {
D d;
d.B::a = 1; // This is working correctly on this path class D -\> class B -\> class A
d.a = 2;
std::cout << d.a;
/*
But This line gives the following error
Non-static member 'a' found in multiple base-class subobjects of type 'A':
class D -> class B -> class A
class D -> class A
*/
return 0;
}

C++ Expand class Base with class Derived that contains a function returning a * b from base class

class Base {
public :
Base ( int a , int b ) : a ( a ) , b ( b ) { }
protected :
int a , b ;
} ;
I have this class called Base, how do I create an inherited class Derived with a function that will multiply protected members a and b?
class Derived : public Base {
public:
void print() {
cout << a * b;
}
};
int main() {
Base b(2, 3);
Derived d;
d.print();
}
This is what I attempted but I get error message ' the default constructor of "Derived" cannot be referenced -- it is a deleted function
The error is because there's no valid Derived constructor.
Do something like this:
class Derived : public Base {
public:
using Base::Base; // Use the Base class constructor as our own
// Rest of Derived class...
};
Then define a single variable of the Derived class:
Derived d(2, 3);
d.print();
Node that with your current code, you attempt to define two different and unrelated variables b and d.
Derived doesn't have a default constructor so you can't do
Derived d;
You could add one though - and bring in the Base constructors while you're at it:
class Base {
public:
Base() : Base(0, 0) {} // now Base has a default ctor
Base(int a, int b) : a(a), b(b) {}
protected:
int a, b;
};
class Derived : public Base {
public:
using Base::Base; // and now Derived can also use it
void print() { std::cout << a * b; }
};

Access inner class private variable in outer class

//In file1.hpp
class A
{
protected:
class B
{
public:
B () {};
};
};
// In file2.hpp
class C
{
public:
void getValue()
{
D obj; ---- error: no matching function for call to D
printf("%d\n",obj.c);
}
class D : public A::B
{
friend class C; -- I tried writing this but still no luck.
public:
D(int a, int b) : c(a), d(b) {}
virtual ~D() {}
//something
private:
int c; int d;
};
class E : public D
{
E() : D(1,2) {}
virtual ~E() {}
};
}
int main()
{
C::E obj;
}
In the public function, getValue I want to access the private member variables of the class D which are (c and d). How can I do that? I tried putting "friend class C" inside class D and then tried creating an object of class D inside getValue function but instead of getting a value like c=5 or d=6, I always get 0.
If I print the value in the following area, I get the correct value. I won't be able to show you how getValue is called but just imagine that it is called somehow. I just need to print c,d in that.
D(int a, int b)
: c(a), d(b) {};
EDIT: At the time of instantiation in getValue, I do something like this
D obj; --- error: no matching function for call to D
Let take the following example:
#include <iostream>
//In file1.hpp
class A
{
protected:
class B
{
public:
B () = default;
};
friend class C; // <-- bad idea
};
// In file2.hpp
class C
{
public:
void getValue()
{
// Creating an object E?
E objE;
// Access c and d
std::cout << "c:" << objE.c << ", d:" << objE.d << std::endl;
}
class D : public A::B // <-- bad idea?
{
public:
D(int a, int b): c(a), d(b) {}
virtual ~D() {}
//something
private:
int c; // dangerous to not initialize basic types
int d;
friend class C; // <-- bad idea
};
class E : public D
{
public:
E() : D(1,2) {}
virtual ~E() {}
};
};
int main()
{
C objC;
objC.getValue();
}
( you can run it here: https://onlinegdb.com/hNfm7Pvg0f )
First is, to have an instance of E to access in C::getValue, so I instantiated an object.
private and protected indicate that those properties and methods are not available publicly (encapsulation) and that is exactly what you are trying to do. You can make exceptions with friend keyword, but that is rarely a good idea (I probably use it twice in my 20 years carrier). But hey! it works.

Inheritance without duplicating code

I have 4 classes, lets say: class A, class B, class C and class D.
Class B inherits from class A.
Class C and class D inherit from class B.
Classes A and B are abstract classes.
I would like to declare a field in class A, lets say int posision and define this field in constructors of class C and class D by assigning value of the parameter (int parameterValue) to this field.
Is there any solution to do this without duplicating line position = parameterValue in all constructors of descendant classes?
You might use inherited constructor:
struct A
{
A(int position) : position(position) {}
virtual ~A() = default;
int position;
};
struct B : public A
{
using A::A;
};
struct C : public B
{
using B::B;
};
struct D : public B
{
using B::B;
};
Demo
Put it in class B and call the super constructor at the begining of the descendent classes. Like that:
class A {
protected:
int position;
};
class B: public A {
public:
B(int parameterValue) : A() {
position = parameterValue;
}
};
class C: public B {
public:
C(int parameterValue) : B(parameterValue) {
}
};

Why output is showing errors

This is a code fro diamond tree problem of multiple inheritance
and according to me this code is cool but it is showing some error on compilation
..help me to figure the error
#include<iostream>
using namespace std;
class A //A Diamond tree problem
{
int x;
public:
A(int i) { x = i; }
void print() { cout << x; }
};
class B: virtual public A
{
public:
B():A(10) { }
};
class C: virtual public A
{
public:
C():A(20) { }
};
class D: public B, public C{
};
int main()
{
D d;
d.print();
return 0;
}
It would be useful to see the error:
In constructor ‘D::D()’:
error: no matching function for call to ‘A::A()’
When using virtual inheritance, the virtual base class must be initialised by the most derived class. In this case, that is D; so in order to be able to instantiate D, it must initialise A:
class D: public B, public C
{
public:
D():A(42) {}
};
Alternatively, you could provide A with a default constructor. Declaring any constructor will prevent the compiler from implicitly generating one for you.
You need to provide default construct for D and call A in member initialize list:
class D: public B, public C{
public:
D():A(30){}
};
Or you could provide a default A constructor
A():x(0) {}