• Advertisement
Sign in to follow this  

Class will not compile: error C2226: syntax error : unexpected type 'std::string'

This topic is 4306 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

I've designed an Image class using SDL that is designed to make image management and display simpiler. However, I'm getting a rather strange error message that I can't figure out. I hate asking for help on compiler errors, but I can't figure it out and Google sheds no light on the issue. The error is pointing to the first constructor function. I've tried getting rid of the default parameters, and that seems to fix it, but then it points the same error to the constructor's implementation. Class declaration:
#include <string>
#include "SDL.h"
#pragma once

class Image
{
protected:
	SDL_Surface*	sImage;
	SDL_Surface*	sSource;
	SDL_Rect	rSizeLocation;

public:
	Image(const std::string& strImage = "", const SDL_Surface* sInputSurface = NULL, const SDL_Rect& rInputSizeLocation = {0, 0, 0, 0});
	Image(const Image& imgInput);
	virtual ~Image(void);
	virtual void Initialize(const std::string& strImage = "", const SDL_Surface* sInputSurface = NULL, const SDL_Rect& rInputSizeLocation = {0, 0, 0, 0});
	virtual void Display(void) const;
	virtual const Image& operator=(const Image& imgInput);
	SDL_Rect GetSizeLocation(void) const;
	const SDL_Surface* GetSurface(void) const;
};
Constructor implementation:
#include "SDLTools.h"

Image::Image(const std::string& strImage, const SDL_Surface* sInputSurface, const SDL_Rect& rInputSizeLocation) 
	 : sSource(sInputSurface), rSizeLocation(rInputSizeLocation), sImage(SDL_LoadBMP(strImage.c_str()))
{
}

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I did try to solve your problem by staring at the code, looking for some obvious error, but failed. Sorry. Thought I'd post anyways to inform you that #pragma once not is a valid replacement for include guards.

Share this post


Link to post
Share on other sites
Try putting the "#include <string>" under the "#include 'SDL.h'" Sometimes big libraries like SDL cause a ruckus when you include standard library stuff before them. Also, I think the #pragma once should be above the include directives (but I could be wrong; I never use that directive).

Share this post


Link to post
Share on other sites
I remember you posted this a short while ago - I meant to revisit it but obviously forgot. What I hadn't spotted before was your third parameter: const SDL_Rect& rInputSizeLocation = {0, 0, 0, 0}. This is not legal. What you can do instead is declare a static member, namespace-scope or anonymous zeroed SDL_Rect and use that as your default parameter:
namespace
{

SDL_Rect zeroed_rect = {0, 0, 0, 0};

}

class Image
{
protected:
SDL_Surface* sImage;
SDL_Surface* sSource;
SDL_Rect rSizeLocation;

public:
Image(const std::string& strImage = "", const SDL_Surface* sInputSurface = NULL, const SDL_Rect& rInputSizeLocation = zeroed_rect);
Image(const Image& imgInput);
virtual ~Image(void);
virtual void Initialize(const std::string& strImage = "", const SDL_Surface* sInputSurface = NULL, const SDL_Rect& rInputSizeLocation = zeroed_rect);
virtual void Display(void) const;
virtual const Image& operator=(const Image& imgInput);
SDL_Rect GetSizeLocation(void) const;
const SDL_Surface* GetSurface(void) const;
};


Σnigma

Share this post


Link to post
Share on other sites
Alright, I tried some suggestions, and here's what I got. I'm gonna leave the #pragma once in for now, as I doubt my problem has anything to do with redefinitions at the moment. Also, I'm pretty sure #pragma once goes after the includes, so I'm gonna leave it at the bottom for now.

The error has shifted to pointing to the constructor implementation header:
error C2226: syntax error : unexpected type 'std::string'

My current code:
#include "SDL.h"
#include <string>
#include <vector>
#pragma once

SDL_Rect RectZero = {0, 0, 0, 0};

class Image
{
protected:
SDL_Surface* sImage;
SDL_Surface* sSource;
SDL_Rect rSizeLocation;

public:
Image(const std::string& strImage = "", const SDL_Surface* sInputSurface = NULL, const SDL_Rect& rInputSizeLocation = RectZero);
Image(Image& imgInput);
virtual ~Image(void);
virtual void Initialize(const std::string& strImage = "", const SDL_Surface* sInputSurface = NULL, const SDL_Rect& rInputSizeLocation = RectZero);
virtual void Display(void) const;
virtual const Image& operator=(Image& imgInput);
SDL_Rect GetSizeLocation(void) const;
const SDL_Surface* GetSurface(void) const;
};






#include "SDLTools.h"
Image(const std::string& strImage, const SDL_Surface* sInputSurface, const SDL_Rect& rInputSizeLocation) //This is the line the compiler is currently complaining about
: sSource(sInputSurface), rSizeLocation(rInputSizeLocation), sImage(SDL_LoadBMP(strImage.c_str()))
{
}




Share this post


Link to post
Share on other sites
I think you constructor needs to be declared

Image::Image(blah....)

Also try removing the const before std::string&

personally I never use pragma once for header protection

Share this post


Link to post
Share on other sites
BlueHabu is right on the first point, but the const is necessary (since otherwise you can't provide a string literal as a default argument). Also:
  • You should never use #pragma once on its own. Always use it in conjunction with normal #define header guards (being careful to use legal identifiers) as #pragma once is non-portable.

  • If you're going to make RectZero namespace scope or global then you need to declare it in the header (extern SDL_Rect RectZero;) and define it in the source file (SDL_Rect RectZero = {0, 0, 0, 0};) like any other namespace scope or global variable or else you will get multiple-definition errors.

  • The signature of your copy-assignment operator is non-standard. The standard signature is Class & operator=(const Class &).


Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by BlueHabu
I think you constructor needs to be declared

Image::Image(blah....)

Also try removing the const before std::string&

personally I never use pragma once for header protection


Holy crap you're right! I had tried removing the const, but I didn't see that I had forgotten the scope operator. Thanks.

Added: thanks Enigma, I'm working on the elements you mentioned right now.

Added again: As long as I've got this thread, I might as well ask this question:

I'm getting the following error for my Display method:
error C2664: 'SDL_UpperBlit' : cannot convert parameter 4 from 'const SDL_Rect *__w64 ' to 'SDL_Rect *'

This seems a little strange. Here's the implementation:
void Image::Display(void) const
{
if(sImage != NULL && sSource != NULL)
SDL_BlitSurface(sImage, NULL, sSource, &rSizeLocation); //This is the line in question
}


rSizeLocation was declared as:
SDL_Rect rSizeLocation;

[Edited by - Andorien on April 5, 2006 12:38:57 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Andorien
rSizeLocation was declared as:
SDL_Rect rSizeLocation;


Declaring the member function as const will make 'this' and all its members const as well.

Share this post


Link to post
Share on other sites
after 7 years of C++ dev the whole const keyword confuses the crap out of me -- I guess i need to make it a point to understand it completely.

Everytime in the past when i have "gotten serious" about protecting parameters it seems if i make one thing const then i need to make every thing const.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement