normally I think that a constructor with an initializer list would work if
there were parameters involved but I am reading the book
SFML Game Development with examples and saw a header file
that did not include a parameter in the constructor,tried
recreating the whole thing but it didn't work
If he needed to do more work to initialize his Venus object (that is, Venus has no default constructor), then he'd have a problem with his getBlockSize() call. Solution would be to properly initialize his Venus object before using it for other stuff:
#include <iostream>
class Mercury
{
public:
Mercury(int block_size) {}
};
class Venus
{
public:
Venus(int someparam) {} // 1. If this line is here (no default constr)
int GetBlockSize() { return 500; }
};
class Mars
{
public:
Mars() :
venus_(24), // 2. Then something like this would be needed
mercury_(venus_.GetBlockSize())
{
}
private:
Mercury mercury_;
Venus venus_;
};
int main()
{
Mars m;
return 0;
}
I get the feeling that OP is trying to initialize m_mercury member vaiables when he initates a Mars object. And he is trying to use some data provided by m_venus which is a member of Mars and not initialized yet.
This remindes me of a chicken and egg senario. Which came first?
What you can't see here is that Venus has a member called Mars.
Note that members are initialized in the order they are defined in the class definition, not in the order they appear in the member initializer list. This means that m_mercury is initialized before m_venus (even in icy1's code) which is a problem because you use m_venus to initialize m_mercury.
error: no matching function to call to 'Mercury::Mercury(int)'
This means that there is no Mercury constructor that takes an int as argument.
#include <iostream>
usingnamespace std;
class Venus
{
char a;
char b;
public:
//A constructor for Venus. It takes parameters!
Venus(char x, char y) : a(x), b(y) {}//Sets a to equal x and b to equal y.
void geta() {
cout << a;
}
void getb() {
cout << b;
}
};
class Mars
{
Venus venus;
int one;
public:
Mars(); //This is a default constructor. No parameters.
void showStuff() {
cout << one << endl;
venus.geta();
cout << "\t";
venus.getb();
}
};
//Here we define the constructor and use an initailization list
Mars::Mars() : venus('A', 'B') {//Our Venus constructor is called.
one = 1;
}
int main()
{
Mars m;
m.showStuff();
return 0;
}