Sign in to follow this  

CPP Class Confusion - SOLVED, thanks ToohrVyk!

This topic is 4142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Over the past week I've been trying to get my head around using classes in CPP. So far I've written a small SDL program to create a 2d starfield effect, which works fine- I create Star objects, which are then aggregated into a Starfield object so I can dial in as many stars as I might want. However, I've been attempting to add a class Ship, so I can have a little ship zooming around- and here's where Ive hit a bit of a snag. When I attempt to compile I get boatloads of compiler errors, and I'm not sure why as I'm doing it pretty much the same as with my other classes. I've noticed that when I comment out Ship's constructor it compiles, so this is where I presume the problem is. Find my classes.h file below, with the offending class commented. Knowing me it'll be some silly & obvious little syntax error I've made somewhere! By the way, I'm using VCPP EE. Here are my compiler errors: Error 1 error C2062: type 'int' unexpected c:\program files\seb\sdlprojects\shooter\ship\ship\ship\classes.h 70 Error 2 error C2334: unexpected token(s) preceding ':'; skipping apparent function body c:\program files\seb\sdlprojects\shooter\ship\ship\ship\classes.h 70 Error 3 error C2143: syntax error : missing ')' before '}' c:\program files\seb\sdlprojects\shooter\ship\ship\ship\classes.h 72 Error 4 error C2143: syntax error : missing '}' before ')' c:\program files\seb\sdlprojects\shooter\ship\ship\ship\classes.h 72 (plus lots more, they're all syntax errors similar to this one) Here's my classes.h file. The problem is the Ship class, at the bottom:
class Star
{
public:
	Star(int x = (rand() % WINDOW_WIDTH), float y = (rand() % WINDOW_HEIGHT),
		float brightness = (rand() % 255), float m_Speed = 0):
	  m_XPos(x + 1), m_YPos(y), m_Brightness(brightness), m_Speed(brightness / 255)
	  {}

	void UpdatePos()
	{
		if (m_YPos >= WINDOW_HEIGHT)
		{
			m_XPos = (rand() % WINDOW_WIDTH);
			m_YPos = 0;
		}
		else
			m_YPos += m_Speed;
	}

	void PlotStar(SDL_Surface* screen)
	{
		pixelRGBA (screen,
		m_XPos, (Sint16)m_YPos,
		(Uint8)m_Brightness, (Uint8)m_Brightness, (Uint8)m_Brightness,
		ALPHA);
	}

private:
	int m_XPos;
	float m_YPos;
	float m_Brightness;
	float m_Speed;

};


class Starfield
{
public:
	
	Starfield (int numStars = NUM_STARS)
	{
		m_StarVector.reserve(numStars);
	}
	
	void AddStars(Star& aStar)
	{
		m_StarVector.push_back(aStar);
	}

	
	void DrawStars(SDL_Surface* screen)
	{
		for(vector<Star>::iterator iter = m_StarVector.begin();
			iter != m_StarVector.end(); ++iter)
		{
			iter->UpdatePos();
			iter->PlotStar(screen);
		}
	}

private:
	vector<Star> m_StarVector;
};

class Ship
{
public:
	
	//problem constructor, if I comment this out it compiles
	Ship(int x = ((WINDOW_WIDTH / 2) - (SPRITE_SIZE / 2)), int y = (WINDOW_HEIGHT - SPRITE_SIZE):
	  m_XPos(x), m_YPos(y)
	  {}

	  //NB variables in caps are all global constants
	  

private:
	int m_XPos;
	int m_YPos;
};


If anyone can help then I'll be eternally in your debt! I can provide the rest of my project's code on request. Cheers! Sven

Share this post


Link to post
Share on other sites

This topic is 4142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this