I am currently developing a c++ program with the main purpose being you have two different objects which float around the screen, crash into each other, etc. Anyway the problem that I am having is that I need to derive two classes from my base class. However in the definition and declaration of my derived class I am getting an error and cant work it out. I have searched the net and sought advice from my colleagues however am unable to find the source of the problem. the code is
Jetsam(RandomNumber &rnd, Console &console);
(For the header file)
and
Jetsam::Jetsam(RandomNumber &rnd, Console &console): Element(rnd, console){};
(For the cpp file)
The error I am getting is IntelliSense:
Jetsam::Jetsam(RandomNumber &rnd, Console &console)" provides no initializer for: e:\c++\my game\my game\jetsam.cpp.
Does anyone have any idea of what is wrong. Any help would be much appreciated :)
Cheers guys, Alyn.
As requested:
JETSAM
#pragma once
#include "RandomNumber.h"
#include "Console.h"
#include "element.h"
#include <iostream>
using namespace std;
class Jetsam : public Element
{
public:
Jetsam(RandomNumber &rnd, Console &console);
~Jetsam();
void printAt(void);
protected:
RandomNumber &rnd;
Console &console;
};
Element
#pragma once
#include "RandomNumber.h"
#include "Console.h"
#include <iostream>
using namespace std;
// code shell, amend as appropriate
class Element
{
protected:
RandomNumber &rnd;
Console &console;
int x;
int y;
int energy;
int direction;
int speed;
char identifier;
static char nextIdentifier;
public:
Element();
Element(RandomNumber &rnd, Console &console);
// precondition: references to RandomNumber and Console objects are provided, along with any other desired parameters
// postcondition: Element object created and private members initialised
// example of use: Element element(rnd, console);
virtual void print(void);
// precondition: none
// postcondition: identifier, x and y are sent to the standard output
// example of use: element.print();
virtual void printAt(void)=0;
// precondition: none
// postcondition: text background is changed proportionate to its energy in the following order
// BLUE, GREEN, AQUA, YELLOW, RED, PURPLE, e.g. an object with 23 energy would have an AQUA background
// object's identifier is sent to the standard output at its x, y coordinates
// example of use: element.printAt();
int getX(void);
int getY(void);
int getEnergy(void);
int getDirection(void);
int getSpeed(void);
//getters for the base class
void setX(int);
void sety(int);
void setEnergy(int);
void setDirection(int);
void setSpeed(int);
//setters for the base class
};
1 Answer 1
The problem is that both the classes Element and Jetsam define these members:
RandomNumber &rnd;
Console &console;
This means each instance of Jetsam actually has two of these: Element::rnd and Jetsam::rnd. As they're references, they have to be initialised in the mem-initialiser list of the constructor; there's no other way to initialise them.
To simply fix the error, you'd have to do this:
Jetsam::Jetsam(RandomNumber &rnd, Console &console) :
Element(rnd, console)
, rnd(rnd)
, console(console)
{}
However, I suspect you don't really want them duplicated (especially since they're protected inside Element). So the correct solution would be to simply remove their declarations from Jetsam. After that, Jetsam would look like this:
class Jetsam : public Element
{
public:
Jetsam(RandomNumber &rnd, Console &console);
~Jetsam();
virtual void printAt(void);
};
Note that it's generally a good idea to repeat the virtual keyword when overriding virtual functions in derived classes (it helps readability), even though it's not required by the standard. Of course, with C++11 compilers which support it, override is even more preferable.
Element(rnd, console), unlessElementtakes pointer types as arguments.ElementandJetsam; I suspect there's a member inJetsamwhich needs explicit initialisation, such as aconstmember or a reference.