Advertisement

Expression must have class type?

Started by April 07, 2014 03:37 AM
9 comments, last by BHXSpecter 10 years, 9 months ago

Im working on Tile Mapping using SDL2, and so far stuff was going good, but for the life of me I can't figure out what I need to do to solve this problem.

This is the error: Expression must have class type.

Here is the class along with its' header.


#include "Sprite.h"
#include "includes.h"



CSprite::CSprite(SDL_Renderer *passedrenderer, std::string Filepath, int x, int y, int w, int h)
{

	renderer = passedrenderer;
	std::cout<<"Before\n";
	image = IMG_LoadTexture(renderer, Filepath.c_str());
	std::cout<<"after " << image<<std::endl;

	rect.x = x;
	rect.y = y;
	rect.w = w;
	rect.h = h;

}


CSprite::~CSprite()
{
	SDL_DestroyTexture(image);
}

void CSprite::Draw()
{
	SDL_RenderCopy(renderer, image, NULL, &rect);
}
void CSprite::SetX(int X)
{
	rect.x = X;
}
void CSprite::SetY(int Y)
{
	rect.y = Y;
}
void CSprite::SetPosition(int X, int Y)
{
	rect.x = X;
	rect.y = Y;

}
int CSprite::GetX()
{
	return rect.x;
}
int CSprite::GetY()
{
	return rect.y;
}
void CSprite::LoadMap(char *name)
{
	int x, y;
	FILE *Map;
	Map = fopen(name, "rb");


	for (y = 0; y < MAP_MAX_Y;y++)
	{
		for (x = 0; x < MAP_MAX_X;x++)
		{
			fscanf(Map, "%d", &Map.tile[y][x]);
		}
	}
	fclose(Map);
 
	
}
void CSprite::DrawMap()
{
	int x, y;

	for (y = 0; y < MAP_MAX_Y; y++)
	{
		for (x = 0; x < MAP_MAX_X; x++)
		{
			if (&Map.tile[y][x] != 0)
			{

			}
		}
	}
}

Header:


#pragma once
#include "includes.h"
class CSprite
{
public:
	CSprite(SDL_Renderer *passedrenderer,std::string Filepath, int x, int y, int w, int h);
	~CSprite();

	void SetX(int X);
	void SetY(int Y);
	void SetPosition(int X, int Y);
	void Draw();
	void LoadMap(char *name);
	int GetX();
	int GetY();
	void DrawMap();
    #define	MAP_MAX_Y 10;
    #define MAP_MAX_X 10;
    #define TILE_SIZE 32;
private:
	SDL_Texture *image;
	SDL_Rect rect;
	SDL_Renderer *renderer;
	

};

Thanks for any help, I'm going to attempt to solve this again tomorrow, but I've been a bit crushed on time recently.

Which line is giving the error? I'm not seeing anything that jumps out at me so it would help if you said what line and file the compiler is saying the error for. I could be missing it though because of it being 1am right now.

Alright, after messing with the code, I dropped out parts I didn't think was the problem and then changed the SDL calls to simple strings of the same name. After fixing some typos and such on my part, it compiles fine for me. Are you sure the class type error is pointing to that class? I'll need more information before I can rule out anything else like SDL being the problem or it actually being your code.

Advertisement

As BHXSpecter said, we need the exact message and the location in the code.

Usually this message appears if you use dot.member (something.member) on a pointer or something that isn't either struct or class.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Endurion, I think you just solved it. I may have removed the lines that gave the error. His LoadMap and DrawMap functions have &Map.tile[][] which I bet is giving the error. I had removed them because I didn't think they were the problem, but you saying that made this jump out at me instantly.


void CSprite::LoadMap(char *name)
{
	int x, y;
	FILE *Map;
	Map = fopen(name, "rb");


	for (y = 0; y < MAP_MAX_Y;y++)
	{
		for (x = 0; x < MAP_MAX_X;x++)
		{
			fscanf(Map, "%d", &Map.tile[y][x]);  // here?
		}
	}
	fclose(Map);
 
	
}
void CSprite::DrawMap()
{
	int x, y;

	for (y = 0; y < MAP_MAX_Y; y++)
	{
		for (x = 0; x < MAP_MAX_X; x++)
		{
			if (&Map.tile[y][x] != 0) // here? Though, not entirely sure 
                                                  // how it is compiling since Map isn't declared in scope.
			{

			}
		}
	}
}

hello I think it needs to know a header file

Yes, clearly this:

fscanf(Map, "%d", &Map.tile[y][x]);

and this

&Map.tile[y][x] != 0

Are the problems. Could the OP please clarify exactly where the map data is supposed to be being stored? There seems nothing in the Sprite class to store this info, perhaps Map is a global variable? If so, you are shadowing it with the local Map in the load method. But I can't think of any implementation of the global that would make the syntax as posted meaningful.

[On another note] There is no point putting those #define's inside the class like that. They won't be contained in the class namespace. You'd be far better to use static const members, but I'm not sure why these are being defined inside a Sprite class since they relate to the map anyway. There is some confusion going on here with naming of things I think.

Advertisement

Yes, clearly this:

fscanf(Map, "%d", &Map.tile[y][x]);

and this

&Map.tile[y][x] != 0

Are the problems. Could the OP please clarify exactly where the map data is supposed to be being stored? There seems nothing in the Sprite class to store this info, perhaps Map is a global variable? If so, you are shadowing it with the local Map in the load method. But I can't think of any implementation of the global that would make the syntax as posted meaningful.

[On another note] There is no point putting those #define's inside the class like that. They won't be contained in the class namespace. You'd be far better to use static const members, but I'm not sure why these are being defined inside a Sprite class since they relate to the map anyway. There is some confusion going on here with naming of things I think.

Sorry to everyone wondering where the it's actually throwing the error, I posted this before I went to bed and kinda spaced it. Both of those lines are throwing the error. I'm not to confident on using #defines, as Im not very familiar with them, so if there is a better way to achieve my goal, i'm more than open to suggestions/ideas or tuts. No map is not a global variable. This is being done in the sprite class because I didn't want to create any unnecessary classes.

What we are pointing out is that in LoadMap() you call FILE *Map; which isn't a class so &Map.tile[][] is what is giving the error because Map has no tile member. As for the defines, he was meaning to do something like:


const int MAP_MAX_Y = 10;
const int MAP_MAX_X = 10;
const int TILE_SIZE = 32;

The DrawMap() function shouldn't even compile because Map is only in the scope of LoadMap() and not declared in DrawMap() nor is it declared in the class members. This is all going off the code from the first post.

This isn't the best resource, but since you are using SDL,try looking at this tutorial and see if it helps clear up anything ( http://lazyfoo.net/SDL_tutorials/lesson29/index.php ). If not, come back and we can certainly try to help you grasp anything you don't understand.


No map is not a global variable.

Then where is it defined? It isn't in the header of CSprite that you posted. And however it is defined, it is unlikely that &name.member[][] makes sense.


No map is not a global variable.

Then where is it defined? It isn't in the header of CSprite that you posted. And however it is defined, it is unlikely that &name.member[][] makes sense.

Oh, I see what your saying. You are correct I never defined it in the header to be used across both of those functions. im going to try to make a struct named map with int tile in it. Is there a better way to do this?

This topic is closed to new replies.

Advertisement