Hi, I'm new to C++, I learned to program in Java first. I'm having trouble getting this to work. I'm writing a text based version of battleship. It was going well until i tried to set the size of the ship. I'm using an int array to check to see if the ship has been hit. When i try to set the array size in my constructor, the compiler yells are me =[. Could someone please tell me what's wrong?
Ship.h
#ifndef _SHIP_H
#define _SHIP_H
enum possition{
HORIZONTAL, // 0
VERTICAL // 1
};
class Ship {
private:
int x; //x-pos
int y; //y-pos
int size; //length of ship
int pos; // horizontal or vertical
int hit[]; // 0=safe, 1=hit
public:
Ship(int x, int y, unsigned int size, int pos);
bool isHit(int x, int y);
int getX(){return x;}
int getY(){return y;}
int getSize(){return size;}
int getPos(){return pos;}
~Ship();
};
#endif /* _SHIP_H */
Ship.cpp
#include "ship.h"
Ship::Ship(int x, int y, unsigned int size, int pos) { // init values for ship
this->x=x;
this->y=y;
this->size=size;
this->pos=pos;
int dummyArray[size];
this->hit=dummyArray; // I think this is the problem... How do i fix it?
}
bool Ship::isHit(int x, int y){//does a guess hit this ship?
if(getPos()==VERTICAL){
if(getX()!=x)return false; // not in the same column
if(y>getY() && y<getY()+getSize()){
this->hit[y-getY()]=1;
return true;
}
}
if(getPos()==HORIZONTAL){
if(getY()!=y)return false; // not in the same row
if(x>getX() && x<getX()+getSize()){
this->hit[x-getX()]=1;
return true;
}
}
}
Ship::~Ship() {
}
And this is the error I'm getting:
Running "C:\cygwin\bin\make.exe -f Makefile CONF=Debug" in C:\Documents and Settings\Neil\My Documents\NetBeansProjects\Battle Ship
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Documents and Settings/Neil/My Documents/NetBeansProjects/Battle Ship'
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/battle_ship.exe
make[2]: Entering directory `/cygdrive/c/Documents and Settings/Neil/My Documents/NetBeansProjects/Battle Ship'
mkdir -p build/Debug/Cygwin-Windows
rm -f build/Debug/Cygwin-Windows/ship.o.d
g++.exe -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/ship.o.d -o build/Debug/Cygwin-Windows/ship.o ship.cpp
ship.cpp: In constructor `Ship::Ship(int, int, unsigned int, int)':
ship.cpp:10: error: incompatible types in assignment of `int[((unsigned int)((int)size))]' to `int[0u]'
make[2]: *** [build/Debug/Cygwin-Windows/ship.o] Error 1
make[2]: Leaving directory `/cygdrive/c/Documents and Settings/Neil/My Documents/NetBeansProjects/Battle Ship'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/cygdrive/c/Documents and Settings/Neil/My Documents/NetBeansProjects/Battle Ship'
make: *** [.build-impl] Error 2
Build failed. Exit value 2.