Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I keep getting this error when I try to compile: Ship.cpp:10:1: error: prototype

ID: 3902598 • Letter: I

Question

I keep getting this error when I try to compile:

Ship.cpp:10:1: error: prototype for ‘Ship::Ship(std::string, int)’ does not match any in class ‘Ship’
Ship::Ship(string sname, int slength)

Compile command that I am using is:

g++ Ship.cpp BBoard.cpp test.cpp -o ship.out

Codes:

// Ship.hpp

3 #include <iostream>
4 #ifndef SHIP_H
5 #define SHIP_H
6
7 using std::string;
8 // Declaring a class called Ship
9 class Ship
10 {
11
12 private:
13 // Name of the ship
14 string name;
15 // Number of squares it occupies
16 int length;
17 // Number of squares that are hit of the ship
18 int damage;
19
20 public:
21
22 // Constructor for the name and length of the ship
23 Ship(string name, int length);
24
25 // Get methods
26 string getName();
27 int getLength();
28 int getDamage();
29
30 void takeHit();
31 bool Destroyed();
32
33 };
34 #endif
~

// Ship.cpp

1 #include "Ship.hpp"
2
3
4 using std::cout;
5 using std::cin;
6 using std::endl;
7 using std::string;
8
9 // Constructor
10 Ship::Ship(string sname, int slength)
11 {
12
13 name = sname;
14 length = slength;
15 damage = 0;
16
17 }
18
19
20 // Getters
21 string Ship::getName() { return name; }
22 int Ship::getLength() { return length; }
23 int Ship::getDamage() { return damage; }
24
25 // When a ship gets hit print the information
26 void Ship::takeHit() {
27 cout << name << "Is hit ";
28 this->damage++;
29 }
30
31 bool Ship::Destroyed(){
32 return damage == length;
33 }
~

Explanation / Answer

This can be solved by adding "Ship.cpp" on the top of your main.cpp file which contains the main for your program