Jump to content
  • Advertisement
Sign in to follow this  
Hrothgar15

These compiler errors are driving me mad!

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've spent forever trying to figure this out, so I took the last resort and decided to post all of my code here. I'm trying to make a C++ Battleship-type console game, with classes for the board, the boats, and the cursor. When I try to compile, I get the following errors: 5 D:\board.h:8, from board.cpp In file included from board.h:8, from board.cpp 5 D:\board.cpp from board.cpp 14 D:\cursor.h expected `;' before '(' token 24 D:\cursor.h variable or field `blink' declared void 24 D:\cursor.h expected `;' before '(' token D:\Makefile.win [Build Error] [board.o] Error 1 Here are the files: battleboats.cpp
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <ctime>
#include <windows.h>
#include "cursor.h"
#include "board.h"
#include "boat.h"
#include "extra.h"

using namespace std;

enum players {PLAYER1, PLAYER2, NUM_PLAYERS};
const int HUMAN = PLAYER1;
const int COMPUTER = PLAYER2;

Board boatBoard[2];
Board guessBoard[2];

void setUpBoats(int numBoats);
bool getKey(const int& KEY);

int main()
{
    int numBoats;
    int* boatCoords;
    
    numBoats = 1;
    
    Boat boat[NUM_PLAYERS][numBoats];
    
    for(int a = 0; a < NUM_PLAYERS; a++)
    {
        for(int b = 0; b < numBoats; b++)
        {
             boat[a] = Boat(3);
        }
    }
        
    for(int i = 0; i < numBoats; i++)
    {
        boatBoard[PLAYER1] = Board(6,6);
        Cursor cursor(0, 0, BLOCK);
        boatCoords = cursor.select(boatBoard[PLAYER1]);
        boatBoard[PLAYER1].addBoat(boat[PLAYER1], boatCoords[0], boatCoords[1], HORIZONTAL);
    }

    cout << "Player's ship board\n";
    boatBoard[PLAYER1].display();
    system("PAUSE");
    return 0;
}





board.cpp
//board.cpp
//Implementation file for class Board

#include <iostream>
#include "board.h"

Board::Board()
{
              
}

Board::Board(int rows, int columns)
{
    m_Rows = rows;
    m_Columns = columns;
    
    for(int a = 0; a < columns; a++)
    {
        for(int b = 0; b < rows; b++)
        {
            m_Row.push_back(EMPTY);
        }
        m_Spaces.push_back(m_Row);
        m_Row.clear();
    }
}

void Board::display()
{
    for(int a = 0; a < m_Rows; a++)
    {
         for(int b = 0; b < m_Columns; b++)
         {
              cout << m_Spaces[a];
         }
         cout << endl;
    }     
    //cout << "The board was displayed.\n\n";
}

void Board::addBoat(Boat& boat, int row, int column, const int& ORIENTATION)
{
    switch(ORIENTATION)
    {
        case HORIZONTAL:
        {
            for(int i = 0; i < boat.getLength(); i++)
            {
                m_Spaces[row][i + column - 1] = boat.getSymbol();
            }
            break;
        }
    }
} 





board.h
//board.h
//Header file for class Board

#ifndef BOARD_H
#define BOARD_H
#include "boat.h"
#include "boatinfo.h"
#include "cursor.h"
using namespace std;

class Board
{
    public:
        Board();
        Board(int rows, int columns);
        void display();
        void addBoat(Boat& boat, int row, int column, const int& ORIENTATION);
        int getRows() const {return m_Rows;}
        int getColumns() const {return m_Columns;}
        char getSpace(int row, int column) const {return m_Spaces[row][column];}
        void setSpaces(int row, int column, char symbol) {m_Spaces[row][column] = symbol;}
        
    private:
        int m_Rows;
        int m_Columns;
        vector<char> m_Row;
        vector< vector<char> > m_Spaces;
             
};   
                         
#endif





boat.cpp
//boat.cpp
//Implementation file for class Boat

#include <iostream>
#include "boat.h"

using namespace std;

Boat::Boat(int length)
{
    m_Length = length;
    m_Symbol = 'B';                                  
}





boat.h
//boat.h
//Header file for class Boat

#ifndef BOAT_H
#define BOAT_H

class Boat
{
    public:
        Boat(int length = 3);
        int getLength() const {return m_Length;}
        int getSymbol() const {return m_Symbol;}
        
    private:
        int m_Length;
        char m_Symbol;
             
};   
                         
#endif





boatinfo.h
#include <windows.h>
#include <conio.h>
#include <vector>
using namespace std;

const int HORIZONTAL = 0;
const int VERTICAL = 1;
const int DELAY = 200000;
const char EMPTY = '-';
const char BLOCK = 219;
const char NONE = 'X';
const char UP = 72;
const char RIGHT = 'M';
const char DOWN = 'P';
const char LEFT = 'K';

int frame = 0;





cursor.h
//cursor.h
//Header file for class Cursor

#ifndef CURSOR_H
#define CURSOR_H
#include "board.h"
using namespace std;

class Cursor
{
    public:
        Cursor();
        Cursor(int row, int column, char symbol);
        int* select(Board& board);

    private:
        int m_XMin;
        int m_XMax;
        int m_YMin;
        int m_YMax;
        int m_Row;
        int m_Column;
        char m_Symbol;
        void blink(Board& board, char off);
        bool getKey(const int& KEY);        
             
};   
                         
#endif






cursor.cpp
//cursor.cpp
//Implementation file for class Cursor

#include <iostream>
#include "cursor.h"
#include "board.h"

using namespace std;

Cursor::Cursor()
{
}

Cursor::Cursor(int row, int column, char symbol)
{
    cout << "A new cursor has been created!\n\n";
    m_Row = row;
    m_Column = column;
    m_Symbol = symbol;
}

int* Cursor::select(Board& board)
{
    static int array[2];
    cout << "The cursor is selecting.\n\n";
    bool ready = true;
    char blinking = NONE;
    if(kbhit())
    {
        while(kbhit())
        {
        }
    }
    while(!getKey(VK_SPACE))
    {
        if(blinking == NONE)
        {
            blinking = board.getSpace(m_Row, m_Column);
        }
        blink(board, blinking);
        if((getKey(VK_UP) || getKey(VK_RIGHT) || getKey(VK_DOWN) || getKey(VK_LEFT)) && ready)
        {
            board.display();
            frame = -1;
            if(getKey(VK_UP))
            {
                m_Row--;
            }
            if(getKey(VK_RIGHT))
            {
                m_Column++;
            }
            if(getKey(VK_DOWN))
            {
                m_Row++;
            }
            if(getKey(VK_LEFT))
            {
                m_Column--;
            }
            ready = false;
        }
        if(!getKey(VK_UP) && !getKey(VK_RIGHT) && !getKey(VK_DOWN) && !getKey(VK_LEFT))
        {
            ready = true;
        }
        frame++;
    }
    array[0] = m_Row;
    array[1] = m_Column;
    return array;
}

void Cursor::blink(Board& board, char off)
{
    if((frame % DELAY) < (DELAY / 2))
    {
        board.setSpaces(m_Row, m_Column, m_Symbol);
    }
    else
    {
        board.setSpaces(m_Row, m_Column, off);
    }
    if((frame % DELAY) == 0 || (frame % DELAY) == (DELAY / 2))
    {
        board.display();
    }
}

bool Cursor::getKey(const int& KEY)
{
    return GetAsyncKeyState(KEY);
}





extra.cpp
//extra.cpp
//Implementation file for extra.h

#include <iostream>
#include "extra.h"
#include <windows.h>
#include <conio.h>
using namespace std;

void pause()
{
    if(kbhit())
    {
        while(kbhit())
        {
        }
    }
    getch();
}

int getInput(string output, int min, int max)
{
    string stringChoice;
    int choice = 0;
    cout << output + " " << endl;
    while(choice < min || choice > max)
    {
        cin >> stringChoice;
        choice = atoi(stringChoice.c_str());
        if(choice < min || choice > max)
        {
            cout << "\nInvalid input.\n" << endl;
            cout << output << endl;
        }
        return choice;
    }
}





extra.h
//extra.h
//Header file to define extra functions

#include <string>
#ifndef EXTRA_H
#define EXTRA_H
using namespace std;

void pause();
int getInput(string output, int min, int max);
                         
#endif

Can someone tell me what the problem is here? [Edited by - Hrothgar15 on July 20, 2005 2:52:51 PM]

Share this post


Link to post
Share on other sites
Advertisement
Either your compiler is braindead, or there's more going on there than you've posted...in any case, cursor.h and boatinfo.h contain duplicate definitions. That's both a waste of typing and a source of compiler error, since you're including them both from board.h. No include guards on either one, either...

Share this post


Link to post
Share on other sites
Well, to learn how to use headers and such to best effect, read this article. It goes into a lot of detail (i.e. a long read), but that's how the people who know what they're doing do it.

In this particular case, you've got the basics for the most part, but get rid of either boatinfo.h or cursor.h, and rename the remaining file to something more suitable to be included in multiple locations. Also it's just good practice to add the include guards (like you have in board.h, boat.h, and extra.h -- you know, the #ifndef/#define/#endif) to every header file you have.

Share this post


Link to post
Share on other sites
Haha...I get a conflict between your select in class Cursor and the select from winsock2.h, weird. In any case, I'm afraid I can't see the solution immediately. HOWEVER...I suspect if you refactor your files according to that article I mentioned, the problem will go away all by itself.

Share this post


Link to post
Share on other sites
OK, I added guards to everything, but now, I get these errors:

multiple definition of `frame'
first defined here

repeated four times. My boatinfo.h file is now this:


#ifndef BOATINFO_H
#define BOATINFO_H
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <ctime>
#include <windows.h>
#include <string>
#include <vector>
using namespace std;

const int HORIZONTAL = 0;
const int VERTICAL = 1;
const int DELAY = 200000;
const char EMPTY = '-';
const char BLOCK = 219;
const char NONE = 'X';
const char UP = 72;
const char RIGHT = 'M';
const char DOWN = 'P';
const char LEFT = 'K';

int frame = 0;

#endif



Is the guard not working or something?

Share this post


Link to post
Share on other sites
Please...read the article...especially the Problem 4. That's what's happening here: you include the header in multiple source files, so there are multiple copies of "frame" floating around, and when the compiler goes to put the program together, it doesn't know which one to use. What the article that I might seem to be shoving down your throat says to do is to put the instantiation of globals (what you're doing right now) into a source (.cpp) file, and have only the definitions in a header. In other words, the header would include the keyword "extern" before the definition, and not assign a value:

extern int frame;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!