Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am getting these two errors below when I am working on an assignment from my instructor, I have also included his test case that

I am getting these two errors below when I am working on an assignment from my instructor, I have also included his test case that the code must be able to run in order to compile. At the bottom of this document is the directions for the entire document. Please can someone fix the error and send it to me. Thank you very much I really appreciate it.

Error 1.

[ 50%] Building CXX object CMakeFiles/runTests.dir/tests.cpp.o tests.cpp: In member function virtual void {anonymous}::MimirTest_cppUnitTest_Test::TestBody(): tests.cpp:22:34: error: no matching function for call to BBoard::placeShip(Ship*, int, int, char) board.placeShip(&ship1, 2, 2, 'R'); ^ In file included from tests.cpp:6:0: BBoard.cpp:70:6: note: candidate: bool BBoard::placeShip(Ship&, int, int, char) bool BBoard::placeShip(Ship& ship, int r, int c, char orientation) { ^~~~~~ BBoard.cpp:70:6: note: no known conversion for argument 1 from Ship* to Ship& tests.cpp:26:48: error: cannot convert Ship to Ship* in initialization Ship* shipPtr = board.getShipsArrayElement(2, 3); ^ CMakeFiles/runTests.dir/build.make:62: recipe for target 'CMakeFiles/runTests.dir/tests.cpp.o' failed make[2]: *** [CMakeFiles/runTests.dir/tests.cpp.o] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed make[1]: *** [CMakeFiles/runTests.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

Input Test Case by Instructor

#include "Ship.cpp"

BBoard board; Ship ship1("Boaty McBoatface", 2); board.placeShip(&ship1, 2, 2, 'R'); ASSERT_EQ(board.allShipsSunk(), false); board.attack(2, 3); ASSERT_EQ(ship1.getDamage(), 1); Ship* shipPtr = board.getShipsArrayElement(2, 3); ASSERT_EQ(shipPtr, &ship1);

Error 2.

Scanning dependencies of target runTests [ 50%] Building CXX object CMakeFiles/runTests.dir/tests.cpp.o tests.cpp: In member function virtual void {anonymous}::MimirTest_cppUnitTest_Test::TestBody(): tests.cpp:24:34: error: no matching function for call to BBoard::placeShip(Ship*, int, int, char) board.placeShip(&ship1, 2, 2, 'R'); ^ In file included from tests.cpp:6:0: BBoard.cpp:70:6: note: candidate: bool BBoard::placeShip(Ship&, int, int, char) bool BBoard::placeShip(Ship& ship, int r, int c, char orientation) { ^~~~~~ BBoard.cpp:70:6: note: no known conversion for argument 1 from Ship* to Ship& CMakeFiles/runTests.dir/build.make:62: recipe for target 'CMakeFiles/runTests.dir/tests.cpp.o' failed make[2]: *** [CMakeFiles/runTests.dir/tests.cpp.o] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed make[1]: *** [CMakeFiles/runTests.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

Input Test Case by Instructor

#include #include "Ship.cpp" std::stringbuf sbuf(std::ios::out); auto oldbuf = std::cout.rdbuf(std::addressof(sbuf)); BBoard board; Ship ship1("Boaty McBoatface", 2); board.placeShip(&ship1, 2, 2, 'R'); board.attack(2, 3); board.attack(2, 2); board.attack(2, 3); ASSERT_EQ(sbuf.str(), "They sank Boaty McBoatface! ");

THIS IS MY CODE BELOW

SHIP.cpp

// define a header file

#include "Ship.hpp"

using std::cout;

using std::cin;

using std::endl;

using std::string;

// create a consructor with argument

Ship::Ship(string name, int length) {

// assign a value

this->name = name;

this->length = length;

damage = 0;

}

// Default Constructor for ship

Ship::Ship() {

// assign a value

this->name = "No Ship";

this->length = 0;

damage = 0;

}

string Ship::getName() {

return name;

}

int Ship::getLength() {

return length; // returns length

}

int Ship::getDamage() {

// returns damage

return damage;

}

void Ship::takeHit() {

// display

std::cout<

//damage

this->damage++;

}

// checks hits

bool Ship::isCompletelyHit() {

return this->damage == this->length;

}

Ship.hpp

#ifndef Ship_hpp

#define Ship_hpp

#include

using std::cout;

using std::cin;

using std::endl;

using std::string;

// Declaration of class Ship

class Ship {

private:

string name;

int length;

int damage;

public:

Ship(string name, int length);

Ship();

// method definition to get

string getName();

int getLength();

int getDamage();

// method definition

void takeHit();

bool isCompletelyHit();

// making a overload operation

void operator=(const Ship &s) {

this->name = s.name;

this->length = s.length;

this->damage=s.damage;

}

};

#endif

BBoard.cpp

// BBoard.cpp

#include "BBoard.hpp"

using std::cout;

using std::cin;

using std::endl;

using std::string;

// Constructor of BBoard class

BBoard::BBoard() {

ships = new Ship**[Rsize]; // create a 2D array of Ship pointers

for(int i = 0; i < Rsize; i++)

ships[i] = new Ship*[Csize];

for(int i = 0; i < Rsize; i++)

for(int j = 0; j < Csize; j++){

attacked[i][j] = false;

Ship s;

ships[i][j] = NULL;

}

unsunkShips = 0;

}

// define a method

bool BBoard::getAttacksArrayElement(int r, int c) {

if(r < Rsize && r >= 0 && c < Csize && c >= 0)

return attacked[r][c];

else

return false;

}

Ship BBoard::getShipsArrayElement(int r, int c) {

if(r < Rsize && r >= 0 && c < Csize && c >= 0)

return *ships[r][c];

else

return Ship();

}

int BBoard::getNumShipsRemaining() {

return unsunkShips;

}

// define a method

bool BBoard::placeShip(Ship& ship, int r, int c, char orientation) {

if(r < Rsize && r >= 0 && c < Csize && c >= 0) {

if(orientation == 'R' && c+ship.getLength() <= Csize) {

for(int i = c; i < c+ship.getLength();i++ )

ships[r][i] = &ship;

unsunkShips++;

return true;

}

else if(orientation == 'C' && r+ship.getLength() <= Rsize) {

for(int i = r; i < r+ship.getLength(); i++)

ships[i][c] = &ship;

unsunkShips++;

return true;

} else

return false;

}

return false;

}

// define a method

void BBoard::attack(int ran, int cc) {

if(ran < Rsize && ran >= 0 && cc < Csize && cc >= 0) {

if(!attacked[ran][cc]) {

attacked[ran][cc] = true;

ships[ran][cc]->takeHit();

if(ships[ran][cc]->isCompletelyHit()) {

cout<<"They Hit "

unsunkShips--;

}

}

}

}

bool BBoard::allShipsSunk() {

return unsunkShips == 0;

}

void BBoard::displayBoard() {

std::cout<<" Display Board ";

std::cout<<" ";

for(int x = 0; x < Csize; x++)

std::cout<<(x)<<" ";

std::cout<

Ship s; // empty ship

for(int x = 0; x < Rsize; x++) {

std::cout<

for(int j = 0; j < Csize; j++) {

if(ships[x][j] == NULL) {

std::cout<<" ";

} else {

std::cout<<"X ";

}

}

std::cout<

}

}

BBoard.hpp

// define a header files

#ifndef BBoard_hpp

#define BBoard_hpp

#include "Ship.hpp"

using std::cout;

using std::cin;

using std::endl;

using std::string;

// implement a class

class BBoard {

private:

// declare static variables

const static int Csize = 10;

const static int Rsize = 10;

// define a variable of array, pointer

bool attacked[Rsize][Csize];

Ship ***ships;

int unsunkShips;

public:

BBoard();

// method defining

bool getAttacksArrayElement(int r, int c);

// method defining

Ship getShipsArrayElement(int r, int c);

// method defining

int getNumShipsRemaining();

bool placeShip(Ship& ship, int r, int c, char orientation);

void attack(int r, int c);

bool allShipsSunk();

void displayBoard();

};

#endif

Main.cpp

// include bboard class

#include "BBoard.hpp"

using std::cout;

using std::cin;

using std::endl;

using std::string;

int main() {

BBoard board;

// create an instance object

Ship s1("Cruiser", 4);

Ship s2("Destroyer", 3);

std::cout<<"Placing 2 Ships in the Board"<

board.placeShip(s1, 6, 8, 'C');

board.placeShip(s2, 4, 2, 'R');

// display the result

board.displayBoard();

std::cout<<" Number of Unsunk Ships : "<

// attack a ship

std::cout<

board.attack(4, 2);

board.attack(4, 3);

board.attack(4, 4);

std::cout<<" Number of Unsunk Ships : "<

}

ASSIGNMENT INSTRUCTION BELOW

For this project you will write a couple of classes that could be used to make a program that plays the game BattleshipLinks to an external site..

The Ship class represents a ship that has:

a name (e.g. "my destroyer", "my submarine", "Boaty McBoatface") a length (the number of squares it occupies) a damage (how many of its squares have been hit) a constructor that takes as parameters (in this order): the Ship's name and the Ship's length, which will be used to initialize the corresponding data members. The damage should be initialized to zero. getMethods for each data member (getName, getLength, getDamage) a method called takeHit that increments a Ship's damage by one The BBoard class represents a 10x10 grid that holds some number of Ships. It should have:

a 10x10 array of bools (for keeping track of what squares have been attacked) a 10x10 array of Ship-pointers (for keeping track of which Ships occupy which squares) a variable that keeps track of the number of ships that remain un-sunk a constructor that initializes each element of the boolArray to false and each element of the Ship-pointer array to NULL (or nullptr if you prefer) a method called getAttacksArrayElement that takes a row and column (in that order) and returns the element at that position of the bool array a method called getShipsArrayElement that takes a row and column (in that order) and returns the element at that position of the ships array a method called getNumShipsRemaining that returns how many ships remain un-sunk a method called placeShip that takes as parameters (in this order): the address of a Ship object, the row and column of the square of the Ship that is closest to (0, 0), and the Ship's orientation (either 'R' if its squares occupy the same row or 'C' if its squares occupy the same column). I give a couple of examples at the end of the specifications. This method will set the elements of the array that the Ship occupies to hold the address of the Ship. If a Ship would not fit entirely on the Board, or if it would overlap any previously placed ships, the ship should not be added to the Board and the method should return false. Otherwise, the ship should be placed on the Board, the number of un-sunk ships should be incremented, and the method should return true.

a method called attack that takes as parameters the row and column of the attack (in that order). If the attack hits a Ship, you should: record the attack in the bool array if that square has not been hit before, you should call that Ship's takeHit method if all of a Ship's squares have been hit, you should print "They sank [insert name of ship here]!" and decrement the number of ships that remain un-sunk (you should only do these once for any ship) return true (even if that square was previously hit) If the attack is not a hit, you should record the attack in the bool array and return false.

a method called allShipsSunk that returns true if all ships on the Board have been sunk, but returns false otherwise. The files must be named Ship.hpp, Ship.cpp, BBoard.hpp, and BBoard.cpp.

Example of the placeShip method - if we have the following values for the parameters:

a Ship that has a length of 4 the row and column are 6 and 8 respectively the orientation is 'C' Then the ship would occupy the following squares:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 x 7 x 8 x 9 x

If we have the following values for the parameters:

a Ship that has a length of 3 the row and column are 4 and 2 respectively the orientation is 'R' Then the ship would occupy the following squares:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 x x x 5 6 7 8 9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

dy dx Find the derivative of the function y=(4x+3)5(2x+1)2.

Answered: 1 week ago