making initializer list work without parameters

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

SFML example:

:::::::Game.h::::::::

#pragma once
#include "Window.h"
#include "World.h"
#include "Snake.h"


class Game {
public:
	Game(); <-no parameters
	~Game();

	void HandleInput();
	void Update();
	void Render();

	sf::Time GetElapsed();
	void RestartClock();

	Window* GetWindow();
private:
	Window m_window;
	sf::Clock m_clock;
	float m_elapsed;

	World m_world;
	Snake m_snake;
	
};


:::::::::::::Game.cpp file::::::::::::


#include "Game.h"


////////the Game constructor works without any parameters


Game::Game() : m_window("Snake", sf::Vector2u(800, 600)),  m_snake(m_world.GetBlockSize()), m_world(sf::Vector2u(800, 600))
{
	m_clock.restart();

	
	
	m_elapsed = 0.0f;

	
}

Game::~Game() {}




:::::::::::my code when I tried recreating it:::::::::

:::::::Mars.h:::::::::

#ifndef MARS_H
#define MARS_H
#include "Mercury.h"
#include "Venus.h"

class Mars
{
    public:
        Mars();



        virtual ~Mars();

    protected:

    private:
        Mercury m_mercury;
        Venus m_venus;

};

#endif // MARS_H


::::::Mars.cpp:::::::

#include "Mars.h"


#include <iostream>

using namespace std;


 //// how to make this work?I already defined getBlockSize(); in the Venus class

Mars::Mars()
: m_mercury(m_venus.getBlockSize())  
{

}



Mars::~Mars()
{
   
}

Last edited on
What does
it didn't work
mean?
It could be that the program did not compile due to some error, or maybe it did compile but he did not get the result he was hoping for.
Yup. Could be all sorts of things. Maybe the OP will come back and tell us. We're not psychic.
Damn it Jim! I'm a coder not a psychic!
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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.
Last edited on
forgot to mention that the error that pops out
is
error: no matching function to call to 'Mercury::Mercury(int)'

what I meant to find out is how did the SFML example make the

m_snake(m_world.GetBlockSize()) work ? if you take a look at Game::Game() at Game.cpp

and Game(); at Game.h no parameters are present for the initiliazer list , so how did it work?,

when I tried doing the same thing

with m_mercury(m_venus.getBlockSize()) I get an error
Last edited on
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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>

using namespace 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;
}
Last edited on
figured it out, all had to do is specify an int in the Mercury constructor and it worked! thanks!
Last edited on
Topic archived. No new replies allowed.