Variable changes in array assignment for no reason?

I am trying to put an object into a vector like this:

1
2
3
std::vector<Chunk> activeChunks = std::vector<Chunk>(9);

		activeChunks[0] = Chunk(sf::Vector2f(playerSpr.getPosition()).x, sf::Vector2f(playerSpr.getPosition()).y, terrain);


here is the relevant part of my chunk class:

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
for (int a = 0; a < width; a++) {
		for (int b = 0; b < height; b++) {
			sf::Sprite square;
			square.setTexture(tex);

			if (tileMap[a + b*width].temp < 40) {
				tileMap[a + b*width].type = Tile::tundra;//Snow for tundra
				square.setTextureRect(sf::IntRect(33, 33, 65, 65));
				tileMap[a + b*width].load(square);
			}

			if (tileMap[a + b*width].temp >= 40 && tileMap[a + b*width].rain < 50) {
				tileMap[a + b*width].type = Tile::desert;// Yellow for desert
				square.setTextureRect(sf::IntRect(0, 33, 32, 65));
				tileMap[a + b*width].load(square);
			}

			if (tileMap[a + b*width].temp >= 40 && tileMap[a + b*width].rain >= 50 && tileMap[a + b*width].rain <= 100) {
				tileMap[a + b*width].type = Tile::plains;// Plains
				square.setTextureRect(sf::IntRect(33, 0, 65, 32));
				tileMap[a + b*width].load(square);
			}

			if (tileMap[a + b*width].temp >= 40 && tileMap[a + b*width].temp <= 190 && tileMap[a + b*width].rain > 100) {
				tileMap[a + b*width].type = Tile::forrest;//Green for Temporate forest
				square.setTextureRect(sf::IntRect(0, 0, 32, 32));
				tileMap[a + b*width].load(square);
			}

			if (tileMap[a + b*width].temp > 190 && tileMap[a + b*width].water > 100) {
				tileMap[a + b*width].type = Tile::rainForrest;
				square.setTextureRect(sf::IntRect(66, 0, 98, 32));
				tileMap[a + b*width].load(square);
			}

			if (tileMap[a + b*width].height < 55) {
				tileMap[a + b*width].type = Tile::water;//blue water
				square.setTextureRect(sf::IntRect(66, 33, 98, 65));
				tileMap[a + b*width].load(square);
			}

			tileMap[a + b*width].setPos((chunkX * 64 + a)*32, (chunkY * 64 + b)*32);

		}
	}
}


The objects inside tileMap hold a texture from sfml and a boolean value that is set to true earlier in the code. In the debugger I can see that the boolean is set to true and the texture loaded. As soon as the section from my chunk class finishes, and the debugger jumps back to this line:

 
activeChunks[0] = Chunk(sf::Vector2f(playerSpr.getPosition()).x, sf::Vector2f(playerSpr.getPosition()).y, terrain);


I check the values of the chunk at activeChunks[0], and the texture is lost and the boolean is false now. There is no code between the end of the execution of the chunk constructor, and jumping back to the line above, so what could possibly be changing these values?
1
2
3
4
5
6
7
8
9
std::vector<Chunk> activeChunks = std::vector<Chunk>(9);
// activeChunks has now 9 default-constructed Chunk objects

Chunk fubar( sf::Vector2f(playerSpr.getPosition()).x,
             sf::Vector2f(playerSpr.getPosition()).y, terrain );
// your code has only unnamed temporary, but I have here a named variable 'fubar'

// assign (aka copy, overwrite) value of fubar to the first object within the vector:
activeChunks[0] = fubar;

Your debugger has shown that you do construct "fubar" perfectly.

What does the copy assignment operator of class Chunk do?


Why do you construct 9 Chunks?
Why the additional call to copy constructor?

Why not:
1
2
3
4
5
6
std::vector<Chunk> activeChunks; // empty vector
activeChunks.reserve( 9 ); // still an empty vector

activeChunks.emplace_back( sf::Vector2f(playerSpr.getPosition()).x,
                           sf::Vector2f(playerSpr.getPosition()).y,
                           terrain ); // vector has one object 
Here is my copy constructor for the chunk class:

1
2
3
4
5
Chunk::Chunk(const Chunk &other) {
	tileMap = other.tileMap;
	chunkX = other.chunkX;
	chunkY = other.chunkY;
}


I am constructing 9 chunks because I require them all, I just didn't include all the redundant code for brevity.

I tried your method and I am still having the same issues. Also, after emplace_back, shouldn't you need to call a chunk constructor instead of just passing in chunk arguments? When I do it your way I get an error saying for argument 1 there is no conversion from chunk to sf::Texture (the type of terrain)

Maybe there is an issue with the scope of my variables or something like that? My Chunk class holds another vector of Tile objects for which I have created a class. The Tile class also inherits from a drawableObject class. All of these classes have copy constructors and overloaded assignment operators so I'm no sure what the issue is still.
vector::emplace_back:
Inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor.


You do show copy constructor.

activeChunks[0] = fubar; is not construction. It is assignment.



1
2
3
4
5
6
7
8
9
10
11
Chunk foo( x, y, t ); // calls constructor
Chunk foo = Chunk( x, y, t ); // calls constructor to create temporary unnamed Chunk and then copy constructor
Chunk bar = foo; // calls copy constructor
bar = foo; // calls assignment

std::vector<Chunk> gaz;
gaz.emplace_back( x, y, t ); // calls constructor in-place
gaz.push_back( foo ); // calls copy constructor
gaz.push_back( Chunk( x, y, t ) ); // calls constructor to create temporary unnamed Chunk and then copy constructor
gaz[0] = bar; // calls assignment
gaz[0] = Chunk( x, y, z); // calls constructor to create temporary unnamed Chunk and then calls assignment 
Topic archived. No new replies allowed.