Battleship game help!

I am nearly done with creating my battleship game. It uses a 10x10 game board and has 5 ships. I am however having problems with my one class where it says that it does not name a type. I've been googling, trying to figure where my problem might be but I'm totally lost now.
If I could get some help that would be most appreciated!

Here is my Game.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef GAME_H
#define GAME_H

class GAME {
public:
    GAME();
    PLAYER& getPlayer(int id);
    void fire(PLAYER&, int, int); //attacks enemy ship
    void writefile() ;
    int userwincount = 0 ;
    int oppwincount = 0 ;

private:
    PLAYER player1 ;
    PLAYER player2 ;

};

#endif /* GAME_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef Player_h
#define Player_h


class PLAYER {
public:
    PLAYER() ;
    PLAYER(int nID) ;
    PLAYER(std::string nType, int nID) ;
    PLAYER(const PLAYER &tempp) ;
    std::string getPlayerType() ;
    void setPlayerType(std::string nType) ;
    int getID() ;
    void setID(int nID) ;
    bool getTurn() ;
    void setTurn(bool nTurn) ;
    BOARD& getBoard() ;
    SHIP& operator()(int, int) ;
    PLAYER& operator = (const PLAYER &p) ;
    bool operator == (const PLAYER &tempp) ;
    bool operator != (const PLAYER&) ;
private:
    BOARD pboard;
    std::string playerType = "";
    int id = -1;
    bool turn = false ;
};
#endif /* Player_h */ 


I am linking all of these files together with one header file named battleship.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef BATTLESHIP_H
#define BATTLESHIP_H

#include <iostream>
#include <cstring>
#include <cmath>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include "Ship.h"
#include "Board.h"
#include "Game.h"
#include "Player.h"

const std::string border = "---------------------------------------------------------\n";
const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";


#endif // BATTLESHIP_H 


I can post any additional code if needed.
Last edited on
One obvious error that I see is that in battleship.h at lines 12-13 is the order of your includes.
game.h users PLAYER, therefore player.h should be included BEFORE game.h.

If that doesn't fix your undefined type, please post the EXACT error messages from your compiler.

Wow! Thanks so much, I'm an idiot!
Now the last thing that I am not sure how to do is to write out to a file where it will output:

The user has won 'x' times.
The CPU has won 'y' times.

where x and y are variables that increment each integer the game ends.

Here's what I have so far but I already know it's wrong as errors come up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void writefile(filename)
{
    fstream win ;
    win.open("filename.txt") ;

    std::string user = "The user has won: " + userwincount + " times." ;
    std::string opp = "The CPU has won: " + oppwincount + " times." ;

    if (!win.fail())
    {

    }

    else
        return 0 ;
}
userwincount and oppwincount either need to be globals (poor idea) or passed in as arguments (better idea).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>

bool writefile (int userwincount, int oppwincount)
{   std::ofstream win;
    win.open ("filename.txt");  // filename argument not used

    if (!win)
    {   std::cout << "Could not open output file" << std::endl;
        return false;   // Tell caller call failed
    }
    win << "The user has won: " << userwincount << " times." << std::endl;
    win << "The CPU has won: " << oppwincount << " times." << std::endl;
    return true;    //  call successful
}



Last edited on
Topic archived. No new replies allowed.