Hi, i have a problem, i am using a library given by my university to create a game, these are my classes and cpp files, and i am getting this error: error C2512: 'Board' : no appropriate default constructor available, pls help.

class Board
{
protected:
	GWindow &Gwin;
	int coorX;
	int coorY;
	int x,y,x2,y2,x3,y3,d;
	int BoxX[20];
	int BoxY[20];
	bool gotBox;
	//int BlackX[5];
	//int BlackY[5];
public:
	Board(GWindow &);
	void createBoard();
	void selectBox();

};

#include "board.h"

class Ship : public Board
{
private:
	GWindow &Gwin;
	string shipName;
	int shipX,shipY;
	int shipHP;
	int shipType;
	int i;
public:
	Ship(GWindow &);
	void getShip(string,int,int,int);
	void drawShip();
	void moveShip();

};

the board.cpp file

#include "stdafx.h"
#include "gwin.h"
using namespace std;

Board::Board(GWindow &g) :
Gwin(g)
{
	coorX=0;
	coorY=0;
	x=0;
	y=0;
	gotBox=false;
	d=0;
}

void Board::createBoard()
{
	for(int i =0;i<16;i++)
	{
		BoxX[i]=x;
		x=x+40;
	}
	for(int i=0;i<12;i++)
	{
		BoxY[i]=y;
		y=y+40;
	}
	Gwin.setPenColour(WHITE);

	for(int i = 0; i<12 ;i++)
	{
		for(int u = 0; u<16;u++)
		{
			Gwin.outlineRectangle(BoxX[u],BoxY[i],BoxX[u]+40,BoxY[i]+40);
		}
	}
	Gwin.refresh();
}

void Board::selectBox()
{

	if(Mouse.isLeftDown()==true) //to get the correct box coordinates
	{
		coorX=Mouse.x;
		coorY=Mouse.y;

		for(int i = 0; i<16 ;i++)
		{
			if(coorX<=BoxX[i])
			{
				x2=BoxX[i-1];
				
				for(int u = 0; u<12; u++)
				{
					if(coorY<=BoxY[u])
					{
						gotBox = true;
						
						y2=BoxY	[u-1];
						break;
					}
				}break;
			}
			
		}	


	}
	 
//	int *BlackY = new int[5];
//	int *BlackX = new int[5];




	/*if(gotBox)
	{

		
		BlackX[d]=x2;
		BlackY[d]=y2;



			Gwin.setPenColour(RED);
			Gwin.outlineRectangle(BlackX[d-1],BlackY[d-1],BlackX[d-1]+40,BlackY[d-1]+40);

		
		Gwin.setPenColour(YELLOW);
		Gwin.outlineRectangle(BlackX[d],BlackY[d],BlackX[d]+40,BlackY[d]+40);

		

		gotBox = false;
		d++;
	}

	Gwin.refresh();*/
	
}

and the ship.cpp file:

#include "ship.h"
#include "stdafx.h"
using namespace std;


Ship::Ship(GWindow &g) :
Gwin(g)
{ //ERROR POINTS TO HERE, BUT ISNT THIS A CONSTRUCTOR?
	i=0;
	shipX=0;
	shipY=0;
	shipHP=0;//counts how many times the ship has been hit
}

void Ship::getShip(string name,int t, int x, int y)
{
	shipType=t;//1 = battleship, 2 = battlecruiser
	shipX=x;
	shipY=y;
	shipName=name;
}

void Ship::drawShip()
{
	int xsize=40;
	int ysize=40;
	if(shipType==1)
	{
		GImage smallShip("images\\smallShip.png");
		//Gwin.drawImage(shipX,shipY,&smallShip);
		GError ge = Gwin.drawImageScaled( shipX, shipY, xsize,ysize, &smallShip);
	}
	else if(shipType==2)
	{
		GImage longShip("images\\longShip.png");
		//Gwin.drawImage(shipX,shipY,&longShip);
		GError ge = Gwin.drawImageScaled( shipX, shipY, xsize,ysize, &longShip);
	}
}

void Ship::moveShip()
{
	
	int *tempX = new int[5];
	int *tempY = new int[5];

	if(x2==shipX && y2==shipY)
	{
		tempX[i]=x2;
		tempY[i]=y2;
		i++;	
	}
	
	delete[] tempX;
	delete[] tempY;
}

im trying to use inheritance to get the value from selectBox() to use in moveShip(), but the error keeps popping up. thanks for the help and any advice about the code too thx

I believe if you specify a constructor other than a default one you must define a default one. So just pop it into the header file of your class (as you only need the MyConstructor {} variety). Then you won't even need it in your implementation file.

i added Board() in my header file:

class Board
{
protected:
	GWindow &Gwin;
	int coorX;
	int coorY;
	int x,y,x2,y2,x3,y3,d;
	int BoxX[20];
	int BoxY[20];
	bool gotBox;
	//int BlackX[5];
	//int BlackY[5];
public:
	Board();
	Board(GWindow &);
	void createBoard();
	void selectBox();

};

then i ran it and came up with unresolved externals.

then i tried this in the cpp file:

Board::Board()
{
}

then it came up with this error : error C2758: 'Board::Gwin' : must be initialized in constructor base/member initializer list

Put Board() {} in the header (no semicolon and the empty braces). Then you will not need to put the definition in the implementation file. I thought the problem was with Ship, though. You may need to do it for both.

i added Board(){} and Ship(){} these errors came up:
1>c:\users\junk33\documents\visual studio 2008\projects\jland\jland\board.h(17) : error C2758: 'Board::Gwin' : must be initialized in constructor base/member initializer list
1> c:\users\junk33\documents\visual studio 2008\projects\jland\jland\board.h(7) : see declaration of 'Board::Gwin'
1>c:\users\junk33\documents\visual studio 2008\projects\jland\jland\ship.h(16) : error C2758: 'Ship::Gwin' : must be initialized in constructor base/member initializer list
1> c:\users\junk33\documents\visual studio 2008\projects\jland\jland\ship.h(9) : see declaration of 'Ship::Gwin'

It's probably unrelated but aren't you inheriting GWin from Board? If so why are you redeclaring it?

As to the main problem, I think that your GWin needs to be initialized (even if there is no value for it) since it is a reference. Try writing the default constructors as Board : Gwin(NULL) {} and Ship : Gwin(NULL) {} Regrettably I'm not sure that even if it works it is necessarily the right approach.

i've got this error now,
error C2440: 'initializing' : cannot convert from 'int' to 'GWindow &'
im so confused

Apologies because I'm at a loss. Since it is a reference it must be initialized in the constructor. I just don't know what you should initialize it to in the default constructor since no arguments are being passed in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.