-1

I have a slight problem where to be honest I am totally confused with derived class constructors and need some help.

I have a base class with the parameterised constructor:

Element::Element(RandomNumber &rnd, Console &console) 
: rnd(rnd), console(console)

and the derived class constructor of

Jetsam::Jetsam(RandomNumber rnd, Console console):Element(rnd,console)

Basically I want to pass the parameters that are used within the base class constructor into the derived class constructor. I have tried a number of different ways and at the moment I am getting the error message 'no initializer for'.

Does anyone have any help for me or could assist me in understanding my problem. Any help would be much appreciated.

Thank you for your time,

Alyn.

edit :

IntelliSense: "Jetsam::Jetsam(RandomNumber rnd, Console console)" provides no         initializer for:  e:\c++\my game\my game\jetsam.cpp   7

edit:

Element::Element(RandomNumber &rnd, Console &console) 
: rnd(rnd), console(console)
{

}

Jetsam::Jetsam(RandomNumber rnd, Console console):Element(rnd,console)
{

}
Alyn
  • 55
  • 1
  • 7

2 Answers2

0

Jetsam::Jetsam(RandomNumber rnd, Console console): Element::Element(rnd,console);

The above code sample should work.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
0

Ref my comment - if your member variables are declared as references, you will need to define your derived constructor like this:

Jetsam::Jetsam(RandomNumber &rnd, Console &console):Element(rnd,console)

For example, the following code compiles ok for me:

// dummy classes for use as member variables
class RandomNumber{};
class Console{};

// base class
class Element
{
public:
    Element(RandomNumber &rnd, Console &console) : rnd(rnd), console(console) {}

    // member variables
    RandomNumber& rnd;
    Console& console;
};

// derived class
class Jetsam : public Element
{
public:
    Jetsam(RandomNumber &rnd, Console &console) : Element(rnd, console) {}
};

I've assumed that your declaration is similar to the above. Can you edit your question if it's significantly different?

Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • This is what I initially used and still had the say error as above – Alyn Apr 08 '13 at 12:15
  • Is there anything else you would like to see, my derived class constructors contains nothing within its body at the moment – Alyn Apr 08 '13 at 12:33
  • Can you show a cut-down copy of your two classes that is similar in size to my code example above and that doesn't compile? – Roger Rowland Apr 08 '13 at 12:37
  • I've edited it slightly, but I unsure as to what else you would like. – Alyn Apr 08 '13 at 12:46
  • If you chang `Jetsam::Jetsam(RandomNumber rnd, Console console):Element(rnd,console)` to `Jetsam::Jetsam(RandomNumber &rnd, Console &console):Element(rnd,console)` it matches my example, which is ok. What error do you get if you do that? – Roger Rowland Apr 08 '13 at 12:49
  • I've done that and get the exact same error as mentioned in the first edit that I mentioned, the solutions that I have been given have not solved this error. – Alyn Apr 08 '13 at 12:55