Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm recently learning to make a 2d game in SFML using a tutorial series on youtube by Suraj Sharma(currently video 57):

https://www.youtube.com/watch?v=kwd_AVCkvXE&list=PL6xSOsbVA1ebkU66okpi-KViAO8_9DJKg&index=57

After 10:14 into the video i receives a bunch of errors in the 'TileMap' class

Here's the code:

TileMap.h:

C++
<pre>#pragma once
#ifndef TILE_MAP_HH
#define TILE_MAP_HH

#include "Tile.h"

class TileMap
{
private:
	float GridSizeF;
	unsigned GridSizeU;
	unsigned Layers;
	sf::Vector2u MaxSize;
	std::vector<std::vector<std::vector<Tile>>>Map;
public:
	TileMap();
	virtual~TileMap();

	//Funcs
	void Update();
	void Render(sf::RenderTarget& target);
};
#endif // !TILE_MAP_HH


TileMap.cpp:

C++
<pre>#include "pch.h"
#include "TileMap.h"

TileMap::TileMap()
{
	this->GridSizeF = 50.f;
	this->GridSizeU = static_cast<unsigned>(this->GridSizeF);
	this->MaxSize.x = 10;
	this->MaxSize.y = 10;
	this->Layers = 1;

	/*Both push_back and resize functions have E0135 error
	which says:
		class "std::vector<std::vector<std::vector<Tile, std::allocator<Tile>>,
		std::allocator<std::vector<Tile, std::allocator<Tile>>>>,
		std::allocator<std::vector<std::vector<Tile, std::allocator<Tile>>,
		std::allocator<std::vector<Tile, std::allocator<Tile>>>>>>"
		has no member "push_back"("resize")
		*/
	//E0020 : x,y,z are undefined
	//E0065 : expected a ';' in x,y,z for loop declaration

	this->Map.resize(this->MaxSize.x);
	for (size_t x = 0; x < this->MaxSize.x; x++)
	{				
		this->Map.push_back(std::vector<std::vector<Tile>>());
		for (size_t y = 0; y < this->MaxSize.y; y++)
		{	
			this->Map.resize(this->MaxSize.x);
			this->Map[x].push_back(std::vector<Tile>());
			for (size_t z = 0; z < this->Layers; z++)
			{	
				this->Map.resize(this->MaxSize.x);
				this->Map[x][y].push_back(Tile(x*this->GridSizeF, y * this->GridSizeF, this->GridSizeF));
			}
		}
	}
}

TileMap::~TileMap()
{
}

//Funcs
void TileMap::Update()
{
}

void TileMap::Render(sf::RenderTarget& target)
{
	for (auto& x : this->Map) {  /*E2291:this range-based 'for' statement requires
					 a suitable 'begin' function and none was found          
				     */
		for (auto& y : x) {
			for (auto& z : y) {
				z.Render(target);
			}
		}
	}
}


Before video 57 the project works perfectly fine.

Can anyone help me ?

What I have tried:

I've checked the precompiled header file in 'TileMap.cpp' and nothing is wrong.
Posted
Updated 15-Nov-19 19:13pm

So go back to the video and ask the author in a comment at the bottom. He will know his code: we don't!
 
Share this answer
 
Comments
TankRockett 13-Nov-19 23:21pm    
Yeah about that...It's been a week and i didn't heard anything from him
OriginalGriff 14-Nov-19 1:55am    
Then he clearly cares about his project and his tutorial: or he's posted a load of stuff he knows little about just to get subscribers.

You're on your own - I'd break out the debugger, and start working it out!
A classical debugging task, so start using the debugger.

Watch out for the creation of TileMaps object that some of them got destroyed and arent flooding up your memory.

tip: split the TileMap() constructor code (the for loop) in some extra function to better see what is going on.
 
Share this answer
 
Quote:
or (auto& x : this->Map) { /*E2291:this range-based 'for' statement requires
a suitable 'begin' function and none was found

Should actually work because Map is a std::vector and the latter does provide a 'suitable' begin.
Anyway you could replace it with
C++
for (size_t n=0; n< Map.size(); ++n)
{
  auto & x = Map[n];
  //...
 
Share this answer
 
Comments
TankRockett 13-Nov-19 23:26pm    
The debugger said '"size_t" is not a type name'
CPallini 14-Nov-19 11:00am    
Either use
#include <cstddef>
or change it to
for (unsigned n = 0; n < Map.size(); ++n)
Sorry for all the troubles,Visual Studio 2019 community was terrible.I've switched to Visual Studio 2019 Professional and the game works fine now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900