Multiple inheritance: I keep geting compile errors - c++

I have a Base class which inherits a class with an empty constructor and i then have another class which inherits a class with an empty constructor but i also want this class to inherit my base class but i keep geting errors my code is below
BmvMessage - Base class
BmvMessage inherits - DboGenBmvMessage
BmvMessageStructure inherits - DboGenBmvMessageStruture
I also want BmvMessageStructure to inherit BmvMessage
HPP
class BmvMessage : public DboGenBmvMessage
{
// code
};
HPP OF CLASS THAT I WANT TO INHERIT WITH
class BmvMessageStructure : public DboGenBmvMessageStructure , public BmvMessage
{
//CODE
};
CPP OF THIS CLASS
BmvMessageStructure::BmvMessageStructure() : DboGenBmvMessageStructure(), BmvMessage()
{
}

Without error information it is hard to figure out the issue. First thing that springs to mind is, do you have a terminating ; on your class definition?
class BmvMessage : public DboGenBmvMessage
{
// code
}; // <-
Otherwise perhaps it is an order of declaration issue - are you #include-ing your base classes in the correct order and are they correctly defined?

You are sure you have all the required headers included before inheriting? Line in which error occurs would be nice.

Related

How do you use std::make_shared to create a smart pointer of a base class type?

What am I doing wrong here? I cannot believe the below doesn't compile. Do you need to use a shared pointer cast or something?
struct Base
{
};
class Child : Base
{
};
int main()
{
std::shared_ptr<Base> p = std::make_shared<Child>();
}
The error I get with Microsoft compiler is
error C2440: 'initializing': cannot convert from 'std::shared_ptr<Child>' to 'std::shared_ptr<Base>'
It does work. It's just that Base has private inheritance.
struct Child : Base{};
or
class Child : public Base{};
are fixes. Interestingly std::static_pointer_cast and company do exist; but you don't need them here. See https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast
Although #Bathsheba's and #divinas's answers solved this issue, none of them explained why is this the solution, and I think it's important for future viewers to know why is it the problem in this case.
When you are trying to create a pointer to the base class Base you are trying to get an access to all of the public members of the base that belong to the derived class (in this case Child). If the derived class privately inherit the base class (or protected inherit it), all of the members that labeled as public in the base class, inside the derived class now labeled as private (or protected). Which means that the attempt to access them as public members will raise an access privileges violation issue. Because the access privileges is known at the compile time, this is a compilation error.
The solve, as mentioned in previous responses, is to inherit the base class using public inheritance. Which is the default inheritance in structs and non-default inheritance in classes.
You should use public inheritance instead:
class Child : public Base
{
};

Not being able to make the include-.hpp-file work with 2 of them

to sum up my problem, lets say I have this:
class Base {};
class Derived_one : public Base {
private:
Polymorphic_vector arr;
public:
const Polymorphic_vector& get_arr() const{
return this->arr;
}
};
class Derived_two : public Base {};
//Polymorphic_vector.hpp
#include "Base.hpp"
class Polymorphic_vector : public std::vector<Base*> { //some overrided f-ns };
class Foo {
//here i want to have the ability of sth like this
//this->at(int position)->get_arr();
//i.e. call get_arr()
};
//end of Polymorphic_vector.hpp
In the polymorphic vector I have pointers to objects of type Derived_one and Derived_two. I want to call get_arr() in Foo. But in order to be able to do that, I should have it as a virtual f-n in class 'Base' and override it in the derived ones. But, since those classes are in different .hpp files, I would have to include "Polymorphic_vector.hpp" in 'Base', because the f-n get_arr() return a value of type Polymorphic_vector. But I already have 'Base' included in 'Polymorphic_vector'. So yeah, thats it :D I would love to receive some feedback about how to solve that problem, thank you.
Turns out forward declaration is the only way to go in this case. Here it didnt work cause the actual classes I have are ~30 and the inheritance is far more complex, but with a little bit of moving around with the functions I managed to solve my problem. Generally though, this could be of help to someone : c++ header files including each other mutually

Making subclass class from base class struct

I have a library given to me where I am supposed to subclass one of its struct for use in my own app. When I do this, it works fine. However, when I change my subclass definition to class instead of struct (and I make sure to public: before everything inside), the compiler (Visual Studio compiler 10) gives me this odd error:
typecast: conversion exists but is inaccessible
The line on which this error occurs looks like this:
LibraryNameSpace::Client c(config_options, &mySublassObject);
I don't understand why a simple change from struct to class creates this error; any compiler-added default constructors would apply to either struct or class, including conversion constructors (if that's the issue here).
Is it perhaps because creating a class subclass from a struct base class is not a good idea?
Members of a 'struct' are public by default, whereas members of a 'class' are private by default. If you do not specify public/private, all members in the 'struct' become private when you change it to 'class'.
Also did you inherit by private or public?
class Subclass : public SuperClass {
public:
// ...
};

Factory Method and Cyclic Dependency

Edit: Thanks folks, now I see my mistake.
If I'm not wrong, because of its nature in factory method there is cyclic dependency:
Base class needs to know subclasses because it creates them, and subclasses need to know base class. Having cyclic dependency is bad programming practice, is not it?
Practically I implemented a factory, I have problem above, even I added
#ifndef MYCLASS_H
#define MYCLASS_H
#endif
I'm still getting
Compiler Error C2504 'class' : base class undefined
And this error disappers when I remove subclass include from base class header.
Solution 1: don't #include the derived class headers in the base class header, only in the base class cpp. The declaration of the factory method should not use the type of concrete classes returned, only the base type.
Solution 2: use a separate factory class (or a factory method within a separate class) to create your objects. Then the cyclic dependency is totally eliminated. This is the preferred way.
The factory shouldn't be a base class of the products.
Base classes never need to know about derived classes.
You need to revisit your pattern description, because I think you might be mixing a couple of different patterns together: if you're using it to create derived classes, then the factory shouldn't be part of the base class. If you're just using it to create various instances of a single class, then it could be a static member of that class.
In the error message you're getting above, the derived classes always need to know the full implementation of the base class. As a design matter, base classes should never know anything about derived classes.
struct Base {
Base * Create(int param);
};
struct Derived0 : public Base {
};
struct Derived1 : public Base {
};
Base * Base::Create(int param) {
switch (param) {
case 0: return new Derived0();
case 1: return new Derived1();
}
You should not try to implement the factory function within the Base class definition. Simply declare it there, and define it after the derived classes definitions.

Can you have protected nested classes in C++?

I have a class that only really ever needed by classes in a certain class hierarchy. I wanted to know if it is possible to nest the class in the highest class's protected section and have all the other classes automatically inherit it?
"Inherit" is the wrong word to use since it has a very specific definition in C++ which you don't mean, but yes you can do that. This is legal:
class A {
protected:
class Nested { };
};
class B : public A {
private:
Nested n;
};
And code that is not in A or something that derives from A cannot access or instantiate A::Nested.