Hi!

I'm trying to use a base class derivative in another derivative class constructor but it's not working. Here's what I have:

class A {
...
};

class B : public class A {
...
};

class C : public class A {
   C(A objA);
}

However when I try to do

C(B);

I get invalid constructor error. Any ideas?

many things wrong here. When inheriting you don't write

class name2: public class name1

but

class name2: public name1

When you try to construct C, B is not an object but class itself! You should make an object of B and then construct C with object of B;

Didn't even notice I had an extra "class" there but it wasn't in my code.

Anyway doing

B b1(something);
C(b1);

I still get the invalid constructor. More precisely: no constructor of type C(B), only constructor of type C(A), which I don't understand since B is a type of A.

.HPP

#include <iostream>
#include <string>

using namespace std;

class Elem {
public:
	Elem();
	Elem(string str);
	string info;
};

class Feuille: public Elem {
public:
	Feuille(string info, double dist);
private:
	double distance;
};

class Noeud: public Elem {
public:
	Noeud();
	Noeud(Elem *FG, Elem *FD, double dist);
	
private:
	Elem *FG,*FD;
	double distance;
};

.CPP

#include "Elems.hpp"

//========ELEM===========//
Elem::Elem(std::string str) {
	this->info = str;
}

Elem::Elem(){
	;
}


//========NOUED===========//
Noeud::Noeud(Elem *FG, Elem *FD, double dist) {
	this->FG = FG;
	this->FD = FD;
	this->distance = dist;
	
}

//========FEUILLE===========//
Feuille::Feuille(string info, double dist) {
	this->info = info;
	this->distance = dist;
}

MAIN

Feuille *F1 = new Feuille("LOL", 0.2);
	Feuille *F2 = new Feuille("OL", 0.1);
	Noeud(F1,F2);

I hope this is not all your code, and I hope you have a function main() in your file main. I did not look over all this code but the only error I see right away is that Noeud(F1,F2); is a part of a class and you call it without a class. Make an object of type Noeud and then call object.Noeud(); or take Noeud out of a class!

1. This is the whole code. It's for testing purposes.
2. Of course I have a main () ;)
3. Noeud(F1,F2) is the parameterised constructor of the Noeud class which is the problem.

My idea is that I have nodes(Noeud) and leafs(Feuille). I want that a node is able to point towards another nodes or towards leafs. That's why both Noeud and Feuille inherit from Elem, so I can be able to create a Node that points to another Node or a node that points to a leaf.
example:

N
 / \
N   N
or
     N
    / \
   L   N
      / \
     L

Edited 5 Years Ago by Swiftle: n/a

Noeud(F1,F2)

Is the above in your main function? You don't have a variable name there.

You need something like this in your main function to create a node (Noeud):

Noeud *N1 = new Noeud(F1, F2, 0.5);

By the way, since you don't actually want instances of Elem -- just instances of Classes that inherit from it -- you should treat it as an Abstract class. If you add functions to the Noeud and Feuille classes, you won't be able to access those functions using FG or FD unless they are virtual functions. Then you define them also in the child classes.

Also, why do you need a separate class for leaves? Aren't they just nodes without leaves (in code, FG and FD would be NULL)?

Edited 5 Years Ago by Mouche: n/a

This question has already been answered. Start a new discussion instead.