Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

VS2k3 "constructors not allowed a return type", but no return is present


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 ender_341   Members   -  Reputation: 136

Like
0Likes
Like

Posted 17 March 2006 - 07:02 PM

When I compile this code I get this Error: d:\Visual Studio Projects\Tile\Tile.cpp(16): error C2533: 'Tile::__ctor' : constructors not allowed a return type what could be causing this error even though there is no return type listed? Tile.cpp
#include <SDL.h>
#include <string>
#include <fstream>

#include "Graphics.h"
#include "Logger.h"
#include "Tile.h"

Tile::Tile( Graphics graph, Uint8 tile_Size, std::string tilefile, std::string tileMapfile, std::string objectfile, std::string objectMapFile )
{
	this->m_Graphics	= graph;
	this->tileSize		= tile_Size;
	this->tileSheet		= this->m_Graphics->loadimage( TileFile );
	this->objectSheet	= this->m_Graphics->loadimage( objectFile );
	this->loadmap( tileMapFile );
	this->loadmap( objectMapFile );
}

Tile::~Tile( void )
{
	SDL_FreeSurface( this->tileSheet );
	SDL_FreeSurface( this->objectSheet );
}
Tile.h
#ifndef TILE_H
#define TILE_H

#include <SDL.h>
#include <string>

#include "Graphics.h"
#include "Logger.h"

class Tile
{
public:
	Tile::Tile( Graphics graph, Uint8 tile_Size, std::string tilefile, std::string tileMapfile, std::string objectfile, std::string objectMapFile );
	~Tile( void );
private:
	void loadmap( std::string filename );

	Graphics *m_Graphics;
	SDL_Surface *tileSheet;
	SDL_Surface *objectSheet;
	Uint8 tileSize;
	Uint8 tileMap[2][2][16][12];
	Uint8 objectMap[2][2][16][12];
}

#endif
[/source

-Matt S.

Ad:

#2 ProgramMax   Members   -  Reputation: 148

Like
0Likes
Like

Posted 17 March 2006 - 07:08 PM

class Tile
{
public:
Tile::Tile( blah );

that's why.
Tile::Tile in the declaration. Should be just Tile.

#3 Deception666   Members   -  Reputation: 182

Like
0Likes
Like

Posted 17 March 2006 - 07:09 PM

Try removing the Tile::Tile from the header.

#4 ender_341   Members   -  Reputation: 136

Like
0Likes
Like

Posted 17 March 2006 - 07:12 PM

thnx for pointing that out. Unluckily that doesn't seem to be what's causing the problem.
-Matt S.

#5 ProgramMax   Members   -  Reputation: 148

Like
0Likes
Like

Posted 17 March 2006 - 07:14 PM

Would you mind also giving us a piece of the other includes there? Graph.h and Logger.h.

There is a chance that Logger.h, being included right before the class is declared, is not ended correctly.

#6 ender_341   Members   -  Reputation: 136

Like
0Likes
Like

Posted 17 March 2006 - 07:24 PM

Here:

Logger.h

#include <string>
#include <iostream>
#include <fstream>

#ifndef LOGGER_H
#define LOGGER_H

#define LOGNAME "game.log"

class Logger
{
private:

struct lg
{
// Member variables filled by the constructor
std::ofstream &out;
std::string module;

// Overload << to write to the ostream
void operator<< ( const std::string &val);

// Construct by timestamping this line
// (and filling in the member variables while we're here)
lg(std::ofstream &out, char *mod);

// Destruct by flushing out the line
~lg();

std::string stamp();
};

public:

static lg log( char *modu )
{
static std::ofstream of( LOGNAME, std::ios::app);
return lg( of, modu);
}

};
#endif




Graphics.h

#ifndef ENGINE_H
#define ENGINE_H

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_TTF.h>

#include <string>

class Graphics
{
public:
Graphics( Uint32 Width, Uint32 Height, std::string Title,
Uint8 bgR, Uint8 bgG, Uint8 bgB,
std::string font_Name, Uint8 font_Size);

~Graphics( void );

void ClearScreen();

SDL_Surface *loadimage( std::string filename );
void closeimage( SDL_Surface *Image );

void beginScene( void );
void endScene( void );

void drawText( std::string text, Uint32 x, Uint32 y,
Uint8 fR, Uint8 fG, Uint8 fB,
Uint8 bR, Uint8 bG, Uint8 bB);

SDL_Surface *drawSprite( SDL_Rect *src, SDL_Rect *dst, SDL_Surface *srcImage, SDL_Surface *dstImage );

void drawSprite( Uint32 srcx, Uint32 srcy, Uint32 dstx,
Uint32 dsty, Uint32 width, Uint32 height, SDL_Surface *srcImage );
void drawSprite( SDL_Rect *dst, SDL_Surface *srcImage );
TTF_Font *getFont( void );
private:
void setBackgroundColor( Uint8 r, Uint8 g, Uint8 b);

// -- Holds the Backgroung Color of the screen for beginScene()
Uint8 backgroundColorRed;
Uint8 backgroundColorGreen;
Uint8 backgroundColorBlue;

// -- Holds the Basic data for the window for quick reference
Uint32 windowHeight;
Uint32 windowWidth;
std::string windowTitle;

SDL_Surface *screen; // -- Holds the main window surface
TTF_Font *font; // -- Holds the font for the game to use
};

#endif




Logger.cpp

#include "Logger.h"
#include <fstream>
#include <ctime>


std::string Logger::lg::stamp()
{
struct tm *ti;
time_t epochtime;

char str[32];

time(&epochtime); // Get the number of seconds since 1970
ti = localtime(&epochtime); // and convert to a date/time structure

sprintf(str,
"[%04d%02d%02d %02d:%02d:%02d]",
ti->tm_year + 1900,
ti->tm_mon + 1,
ti->tm_mday,
ti->tm_hour,
ti->tm_min,
ti->tm_sec);

return std::string(str);
}

Logger::lg::~lg()
{
out.flush();
}

// -- Constructer, outputs time
Logger::lg::lg(std::ofstream &out, char *mod) : out(out)
{
module = mod;
out << std::endl << stamp() << ": " << module << ": ";
}

// Overload << to write to the ofstream
void Logger::lg::operator<< ( const std::string &val)
{
out << val;
}




Graphics.cpp

#include <SDL.h>
#include <SDL_TTF.h>
#include <SDL_image.h>
#include <string>
#include <vector>

#include "Graphics.h"
#include "Logger.h"

Graphics::Graphics( Uint32 Width, Uint32 Height, std::string Title,
Uint8 bgR, Uint8 bgG, Uint8 bgB,
std::string font_Name, Uint8 font_Size)
{
SDL_WM_SetCaption( Title.c_str(), NULL );

if( SDL_Init( SDL_INIT_EVERYTHING ) == -1)
{
Logger::log("Graphics" ) << "Failed to Initialize SDL";
}

if( ( screen = SDL_SetVideoMode( Width, Height, NULL, SDL_HWSURFACE | SDL_DOUBLEBUF ) ) == NULL )
{
Logger::log( "Graphics" ) << "Failed to Initialize screen";
}

this->setBackgroundColor( bgR, bgG, bgB );

if( TTF_Init() == -1 )
Logger::log( "Font" ) << "Failed to Initialize SDL_TTF";
this->font = TTF_OpenFont( font_Name.c_str(), font_Size);
}

Graphics::~Graphics( void )
{
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}

void Graphics::beginScene()
{
SDL_FillRect(screen,
NULL,
SDL_MapRGB(screen->format, backgroundColorRed,
backgroundColorGreen,
backgroundColorBlue));
}

void Graphics::endScene()
{
SDL_Flip(screen);
}

SDL_Surface *Graphics::loadimage( std::string filename )
{
SDL_Surface *loaded = NULL; // -- Temp Space to hold image
SDL_Surface *final = NULL; // -- The Final Surface that is returned

// -- Load the image
loaded = IMG_Load( filename.c_str() );

// -- Error checking to see if it loaded
if( loaded != NULL )
{
// -- Convert final and Copy Image
final = SDL_DisplayFormat( loaded );

// -- Free memory
SDL_FreeSurface( loaded );
}
else if (loaded == NULL)
{
Logger::log("Graphics") << "Failed to image: ";
Logger::log("Graphics") << filename.c_str();
}

// -- return image
return final;
}

void Graphics::closeimage( SDL_Surface *Image )
{
SDL_FreeSurface( Image );
}

SDL_Surface *Graphics::drawSprite( SDL_Rect *src, SDL_Rect *dst, SDL_Surface *srcImage, SDL_Surface *dstImage )
{
SDL_BlitSurface( srcImage, src, dstImage, dst );

delete src;
delete dst;

return dstImage;
}

void Graphics::drawSprite( SDL_Rect *dst, SDL_Surface *srcImage )
{
SDL_BlitSurface( srcImage, NULL, this->screen, dst );
delete dst;
}

void Graphics::drawSprite( Uint32 srcx, Uint32 srcy, Uint32 dstx,
Uint32 dsty, Uint32 width, Uint32 height, SDL_Surface *srcImage )
{
// -- Holds source and destination locations for image transfer
SDL_Rect src;
SDL_Rect dst;

dst.h = src.h = height;
dst.w = src.w = width;
src.x = srcx;
src.y = srcy;
dst.x = dstx;
dst.y = dsty;

SDL_BlitSurface( srcImage, &src, screen, &dst );
}

void Graphics::setBackgroundColor( Uint8 r, Uint8 g, Uint8 b)
{
this->backgroundColorBlue = b;
this->backgroundColorGreen = g;
this->backgroundColorRed = r;
}

void Graphics::drawText(std::string text, Uint32 x, Uint32 y,
Uint8 fR, Uint8 fG, Uint8 fB,
Uint8 bR, Uint8 bG, Uint8 bB)
{
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Color backgroundColor = { bR, bG, bB };

SDL_Surface* textSurface = TTF_RenderText_Shaded(font, text.c_str(),
foregroundColor, backgroundColor);

SDL_Rect textLocation = { x, y, 0, 0 };

SDL_BlitSurface(textSurface, NULL, screen, &textLocation);

SDL_FreeSurface(textSurface);
}

TTF_Font *Graphics::getFont( void )
{
return font;
}



-Matt S.

#7 Riekistyx   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 March 2006 - 07:25 PM

There you go everything what they said.
A few hints...

//Somewhere in declaration space
using std::string;

//local or global example
string something;


of if your lazy and want a real slow piece of junk
//Somewhere in declaration space
using namespace std;



class Tile
{
public:
Tile( Graphics graph, Uint8 tile_Size, std::string tilefile, std::string tileMapfile, std::string objectfile, std::string objectMapFile );
~Tile( void );
private:
void loadmap( std::string filename );

Graphics *m_Graphics;
SDL_Surface *tileSheet;
SDL_Surface *objectSheet;
Uint8 tileSize;
Uint8 tileMap[2][2][16][12];
Uint8 objectMap[2][2][16][12];
};




I hope this helps also... And if you already knew about that and you enjoy doing the long std:: stuff then disregard my std:: crap.

#8 Deception666   Members   -  Reputation: 182

Like
0Likes
Like

Posted 17 March 2006 - 07:29 PM

Nice catch. Didn't even see that one.

#9 ender_341   Members   -  Reputation: 136

Like
0Likes
Like

Posted 17 March 2006 - 07:57 PM

thanks, I've spent forever trying to figure out whats wrong with it.
-Matt S.

#10 Zahlman   Moderators   -  Reputation: 1666

Like
0Likes
Like

Posted 18 March 2006 - 04:41 AM

Quote:
Original post by Riekistyx
of if your lazy and want a real slow piece of junk
//Somewhere in declaration space
using namespace std;


'Using' a namespace does not incur *any* overhead. All it does is instruct the compiler about name lookup, i.e. how to interpret symbols that it couldn't otherwise identify. The only thing that *might* be (negligibly) slower is the compilation process.

#11 Fruny   Moderators   -  Reputation: 1649

Like
0Likes
Like

Posted 18 March 2006 - 05:08 AM

In the CPP file, you include the header right before defining the constructor. You must therefore read the two files as if the header file was literally substituted where the #include is (which is really what happens).

You're missing a semicolon at the end of your class definition in the header

Therefore the compiler sees something like: class Tile { } Tile::Tile() { } , that is, type function name(function parameters) and parses the class definition as indicating the return type of the function.

And constructors are not allowed to specify a return type.

Just add a semicolon at the end of your class definition. It's a very common error. A type definition acts as a type token just as if you had named it later: class Foo {} bar; is the same as class Foo {}; Foo bar;. The closing brace marks the end of the class definition, but the semicolon marks the end of the variable definition list and the end of the statement.

[Edited by - Fruny on March 18, 2006 12:08:12 PM]

#12 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 18 March 2006 - 06:04 AM

You're using a lot of call by values, you should change them to const references to avoid unecessary copying.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS