Help: Strange error, what's the error?

Discussion in 'Mac Programming' started by slooksterPSV, Jun 2, 2006.

  1. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #1
    Here is the error
    Code:
    cLevel.h:19: error: no matching function for call to 'cTile::cTile()'
    cTile.h:21: note: candidates are: cTile::cTile(SDL_Surface*, int, int, int)
    cTile.h:8: note:                 cTile::cTile(const cTile&)
    
    Here's the sources:
    cLevel.h
    Code:
    //Shawn Barnes
    //June 3rd, 2006
    #ifndef __CLEVEL_H_
    #define __CLEVEL_H_
    
    #include <iostream.h>
    #include <fstream.h>
    #include "cTile.h"
    
    class cLevel {
    
    private:
    	SDL_Surface* m_Bitmap;
    	SDL_Surface* m_Window;
    	cTile m_Tiles[WTILE_HEIGHT][WTILE_WIDTH];
    
    public:
    	cLevel(SDL_Surface* window, SDL_Surface* lBitmap)
    	{
    		m_Window = window;
    		m_Bitmap = lBitmap;
    		Load("data/test.txt");
    	}
    
    	~cLevel() { }
    	//asdf
    	void Load(char* level)
    	{
    		ifstream input_stream(level);
    		int dummy[3];
    		int input_tile_type;
    		input_stream >> dummy[0];
    		input_stream >> dummy[1];
    		input_stream >> dummy[2];
    		for(int col = 0; col < WTILE_HEIGHT; col++)
    		{
    			for(int row = 0; row < WTILE_WIDTH; row++)
    			{
    				input_stream >> input_tile_type;
    				m_Tiles[col][row] = cTile(m_Bitmap, row*TILE_WIDTH, col*TILE_HEIGHT, (int)input_tile_type);
    			}
    		}
    	}
    		
    	void Draw()
    	{
    		for(int col = 0; col < WTILE_HEIGHT; col++)
    		{
    			for(int row = 0; row < WTILE_WIDTH; row++)
    			{
    				m_Tiles[col][row].Draw(m_Window);
    			}
    		}
    	}
    
    };
    
    #endif
    
    cTile.h
    Code:
    //cTile by Shawn Barnes
    //Date: June 3rd, 2006
    //Thanks for Aaron Cox for the help
    
    #ifndef __CTILE_H_
    #define __CTILE_H_
    
    class cTile {
    
    private:
    	int m_ImageX;
    	int m_ImageY;
    	int m_Width;
    	int m_Height;
    	int m_Type;
    	int dstX;
    	int dstY;
    	SDL_Surface* m_Tile;
    	
    public:
    	cTile(SDL_Surface* bitmap, int dx, int dy, int type) : 
    	m_Tile(bitmap), dstX(dx), dstY(dy), m_Type(type)
    	{
    		SDL_Rect r = TileTypeRect(type);
    		m_ImageX = r.x;
    		m_ImageY = r.y;
    		m_Width = r.w;
    		m_Height = r.h;
    		m_Type = type;
    		m_Tile = bitmap;
    	}
    	
    	~cTile()
    	{
    		SDL_FreeSurface(m_Tile);
    	}
    	
    	SDL_Rect TileTypeRect(int type)
    	{
    		SDL_Rect tile_rect;
    		
    		switch(type)
    		{
    			case 1:
    				tile_rect.x = 0;
    				tile_rect.y = 0;
    				tile_rect.w = 32;
    				tile_rect.h = 32;
    			break;
    			default:
    			break;
    		}
    		
    		return tile_rect;
    	}
    	
    	int GetType()
    	{
    		return m_Type;
    	}
    	
    	void SetImageLocation(int x, int y)
    	{
    		m_ImageX = x;
    		m_ImageY = y;
    	}
    	
    	void Draw(SDL_Surface* window)
    	{
    		SDL_Rect source = { m_ImageX, m_ImageY, m_Width, m_Height };
    		SDL_Rect destination = { dstX, dstY, m_Width, m_Height };
    		SDL_BlitSurface(m_Tile, &source, window, &destination);
    	}
    	
    };
    
    #endif
    
    If I remove the cLevel constructor (which I need) the error disappears and compiles perfectly.
     
  2. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #2
    What the heck, I can't declare cTile as an array ? its saying I have to initialize it but I can't/you can't. What am I doing wrong here?

    EDIT: I got it, I added cTile() { } after the first instance of cTile(...args...) { ... }
     
  3. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    I haven't been involved with SDL, but I can give you some advice: Do NOT use <iostream.h> and <fstream.h> because they are deprecated. They don't even support all the functions that the newer versions have. Use <iostream> and <fstream> instead. Then, use the std namespace with the command "use namespace std" (in case you need many commands from that namespace).
     
  4. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #4
    Wait there's a difference between iostream.h and iostream? when did this happen?
     
  5. macrumors regular

    Joined:
    Feb 15, 2003
    Location:
    Connecticut
    #5
    You need a default constructor.

    Hi there. The compiler is complaining because you don't have a default cTile::cTile() constructor, and it needs one in order to construct cLevel. The compiler normally produces a default constructor for any given class automatically as long as you haven't defined any other constructors. cTile does have a non-default constructor defined in this case, so cTile::cTile() ceases to exist unless you provide it explicitly. I am not sure I understand why the error goes away when you remove cLevel's constructor, because I think a default cTile::cTile() will still be necessary, but perhaps someone else can weigh in on that.
     
  6. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #6
    If I have two constructors, function overloading, it works.
     
  7. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #7
    It happened when the namespaces were firstly introduced in C++. Yes, there are many differences between <iostream> and <iostream.h> and the later is considered deprecated (otherwise, why would they be named differently?), so it is recommended to avoid using <iostream.h>.

    You can verify that when you try to include <iostream.h> into Xcode, which will complain that iostream.h is deprecated and you shouldn't use it.
     
  8. thread starter macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #8
    If I'm thinking right, this also works with stack and many others.
     
  9. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #9
    yes. Warnings also come out when using old-type STL libraries such as <map.h>. The current way of calling them is to call them with their new names, without the .h extension and using the std namespace.
     

Share This Page