# Issue with class constructor parameters

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

## Recommended Posts

I've designed an Image class made to make sprite handling a bit easier. It uses the SDL and is simply designed to be able to display an image and accept new information about it (to move it around, etc). However, it seems that the constructor doesn't want to compile. Google sheds no light on the issue. The exact error I'm getting is: error C2226: syntax error : unexpected type 'std::string' The complete declaration of my class is as follows:
#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;
};

I'm using Visual Studio .Net 2003 for this project.

##### Share on other sites
Offhand, I don't see anything wrong in the declaration of your constructor. What does the definition look like? Is the compiler error complaining about your header or your implementation?

##### Share on other sites
Quote:
 Original post by AgonyOffhand, I don't see anything wrong in the declaration of your constructor. What does the definition look like? Is the compiler error complaining about your header or your implementation?

It's complaining about the header (or at least, that's the line it's pointing at). Here's the 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 on other sites
I still can't see anything. :-/ I vaguely remember having problems like this in the past, and I vaguely get the impression that they were due to something else entirely. And after searching a bit on the internet, I found some people with similar (but not exactly similar) problems where the issue was with a header file they included. Perhaps something in SDL.h (or one of the headers it includes) got accidentally changed, or causes problems with VC++ 7.1, or is in conflict with some of the stuff you've written. Some of those are obviously not very likely, such as VC++ 7.1 having issues with SDL.

The usual recommendations for these types of problems is to try commenting out #include directives one at a time (and anything else that relies on them) to figure out precisely which header file is causing the problem. I'd initially just comment out #include "SDL.h", comment out all of your class Image code, and make a shell of the class, with just a constructor and the three protected members. The SDL_whatever pointers can be replaced with simple int* pointers or something. If that compiles fine, then you at least know that the error probably has nothing to do with std::string and something to do with SDL.

##### Share on other sites
Well, after following your advice and removing instances of the SDL, the class declaration seems to check out. Of course, I don't like the implications of this, as it would seem to mean that I can't use string with SDL (and I love my strings and vectors). I've tried removing #include <string> on a whim and that did all of nothing, as expected.

Hopefully Google can provide me some answers. If push comes to shove, I may have to implement my own string class.

Thanks for your help though. It's issues like this that sometimes frustrate me and force a drop in my interest.

EDIT: perhaps I spoke too soon. After figuring that SDL simply didn't want to play nice with std::string, I attempted to change the parameters to simple char pointers. However, I end up with the same error, only in the form of

error C2062: type 'const char' unexpected

So now I'm doubly confused.

[Edited by - Andorien on March 31, 2006 10:50:03 AM]

##### Share on other sites
Run the file through the preprocessor (/P for VC++ 8.0) to see what the compiler actually sees.

Σnigma

##### Share on other sites
Somewhat offtopic, I know, but from the same constructor:

Can anyone tell me whether it is legal C++ to provide a default argument of a struct initializer to an argument passed by const reference?
(Last argument on the same line as the error occurs)

##### Share on other sites
Have you tried
#include "SDL.h"#include <string>

##### Share on other sites
Quote:
 Original post by EnigmaRun the file through the preprocessor (/P for VC++ 8.0) to see what the compiler actually sees.Σnigma

Well, I've never outputted a preprocessor log, much less used one to troubleshoot, but I think I've got what you've suggested, with what looks like the relevent information:

#line 98 "c:\\documents and settings\\andorien\\my documents\\sdl-1.2.9\\include\\sdl.h"#line 3 "c:\\documents and settings\\andorien\\my documents\\c++ projects\\breakout sdl\\sdltools.h"#pragma onceclass Image{protected:	SDL_Surface*	sImage;	SDL_Surface*	sSource;	SDL_Rect	rSizeLocation;public:	Image(const std::string& strImage = "", const SDL_Surface* sInputSurface = 0, 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 = 0, 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;};

I'm not quite sure what I'm looking for, as the class declaration looks identical to what I wrote. If I need to look through the whole file (about 9/10 of which is just going through <string> and "SDL.h"), I guess I can, though it's extremely long.

Quote:
 Have you tried#include "SDL.h"#include

I believe I have, let me try again to be sure... yep, no effect.