These compiler errors are driving me mad!

Started by
7 comments, last by TDragon 18 years, 9 months ago
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] = boat.getSymbol();
            }
            <span class="cpp-keyword">break</span>;
        }
    }
} 





</pre></div><!–ENDSCRIPT–><b>board.h</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">//board.h</span>
<span class="cpp-comment">//Header file for class Board</span>

<span class="cpp-directive">#ifndef</span> BOARD_H
<span class="cpp-directive">#define</span> BOARD_H
<span class="cpp-directive">#include</span> <span class="cpp-literal">"boat.h"</span>
<span class="cpp-directive">#include</span> <span class="cpp-literal">"boatinfo.h"</span>
<span class="cpp-directive">#include</span> <span class="cpp-literal">"cursor.h"</span>
<span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> std;

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





</pre></div><!–ENDSCRIPT–><b>boat.cpp</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">//boat.cpp</span>
<span class="cpp-comment">//Implementation file for class Boat</span>

<span class="cpp-directive">#include</span> &lt;iostream&gt;
<span class="cpp-directive">#include</span> <span class="cpp-literal">"boat.h"</span>

<span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> std;

Boat::Boat(<span class="cpp-keyword">int</span> length)
{
    m_Length = length;
    m_Symbol = 'B';                                  
}





</pre></div><!–ENDSCRIPT–><b>boat.h</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">//boat.h</span>
<span class="cpp-comment">//Header file for class Boat</span>

<span class="cpp-directive">#ifndef</span> BOAT_H
<span class="cpp-directive">#define</span> BOAT_H

<span class="cpp-keyword">class</span> Boat
{
    <span class="cpp-keyword">public</span>:
        Boat(<span class="cpp-keyword">int</span> length = <span class="cpp-number">3</span>);
        <span class="cpp-keyword">int</span> getLength() <span class="cpp-keyword">const</span> {<span class="cpp-keyword">return</span> m_Length;}
        <span class="cpp-keyword">int</span> getSymbol() <span class="cpp-keyword">const</span> {<span class="cpp-keyword">return</span> m_Symbol;}
        
    <span class="cpp-keyword">private</span>:
        <span class="cpp-keyword">int</span> m_Length;
        <span class="cpp-keyword">char</span> m_Symbol;
             
};   
                         
<span class="cpp-directive">#endif</span>





</pre></div><!–ENDSCRIPT–><b>boatinfo.h</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-directive">#include</span> &lt;windows.h&gt;
<span class="cpp-directive">#include</span> &lt;conio.h&gt;
<span class="cpp-directive">#include</span> &lt;vector&gt;
<span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> std;

<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span> HORIZONTAL = <span class="cpp-number">0</span>;
<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span> VERTICAL = <span class="cpp-number">1</span>;
<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span> DELAY = <span class="cpp-number">200000</span>;
<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> EMPTY = '-';
<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> BLOCK = <span class="cpp-number">219</span>;
<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> NONE = 'X';
<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> UP = <span class="cpp-number">72</span>;
<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> RIGHT = 'M';
<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> DOWN = 'P';
<span class="cpp-keyword">const</span> <span class="cpp-keyword">char</span> LEFT = 'K';

<span class="cpp-keyword">int</span> frame = <span class="cpp-number">0</span>;





</pre></div><!–ENDSCRIPT–><b>cursor.h</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">//cursor.h</span>
<span class="cpp-comment">//Header file for class Cursor</span>

<span class="cpp-directive">#ifndef</span> CURSOR_H
<span class="cpp-directive">#define</span> CURSOR_H
<span class="cpp-directive">#include</span> <span class="cpp-literal">"board.h"</span>
<span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> std;

<span class="cpp-keyword">class</span> Cursor
{
    <span class="cpp-keyword">public</span>:
        Cursor();
        Cursor(<span class="cpp-keyword">int</span> row, <span class="cpp-keyword">int</span> column, <span class="cpp-keyword">char</span> symbol);
        <span class="cpp-keyword">int</span>* select(Board&amp; board);

    <span class="cpp-keyword">private</span>:
        <span class="cpp-keyword">int</span> m_XMin;
        <span class="cpp-keyword">int</span> m_XMax;
        <span class="cpp-keyword">int</span> m_YMin;
        <span class="cpp-keyword">int</span> m_YMax;
        <span class="cpp-keyword">int</span> m_Row;
        <span class="cpp-keyword">int</span> m_Column;
        <span class="cpp-keyword">char</span> m_Symbol;
        <span class="cpp-keyword">void</span> blink(Board&amp; board, <span class="cpp-keyword">char</span> off);
        <span class="cpp-keyword">bool</span> getKey(<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span>&amp; KEY);        
             
};   
                         
<span class="cpp-directive">#endif</span>






</pre></div><!–ENDSCRIPT–><b>cursor.cpp</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">//cursor.cpp</span>
<span class="cpp-comment">//Implementation file for class Cursor</span>

<span class="cpp-directive">#include</span> &lt;iostream&gt;
<span class="cpp-directive">#include</span> <span class="cpp-literal">"cursor.h"</span>
<span class="cpp-directive">#include</span> <span class="cpp-literal">"board.h"</span>

<span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> std;

Cursor::Cursor()
{
}

Cursor::Cursor(<span class="cpp-keyword">int</span> row, <span class="cpp-keyword">int</span> column, <span class="cpp-keyword">char</span> symbol)
{
    cout &lt;&lt; <span class="cpp-literal">"A new cursor has been created!\n\n"</span>;
    m_Row = row;
    m_Column = column;
    m_Symbol = symbol;
}

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

<span class="cpp-keyword">void</span> Cursor::blink(Board&amp; board, <span class="cpp-keyword">char</span> off)
{
    <span class="cpp-keyword">if</span>((frame % DELAY) &lt; (DELAY / <span class="cpp-number">2</span>))
    {
        board.setSpaces(m_Row, m_Column, m_Symbol);
    }
    <span class="cpp-keyword">else</span>
    {
        board.setSpaces(m_Row, m_Column, off);
    }
    <span class="cpp-keyword">if</span>((frame % DELAY) == <span class="cpp-number">0</span> || (frame % DELAY) == (DELAY / <span class="cpp-number">2</span>))
    {
        board.display();
    }
}

<span class="cpp-keyword">bool</span> Cursor::getKey(<span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span>&amp; KEY)
{
    <span class="cpp-keyword">return</span> GetAsyncKeyState(KEY);
}





</pre></div><!–ENDSCRIPT–><b>extra.cpp</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">//extra.cpp</span>
<span class="cpp-comment">//Implementation file for extra.h</span>

<span class="cpp-directive">#include</span> &lt;iostream&gt;
<span class="cpp-directive">#include</span> <span class="cpp-literal">"extra.h"</span>
<span class="cpp-directive">#include</span> &lt;windows.h&gt;
<span class="cpp-directive">#include</span> &lt;conio.h&gt;
<span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> std;

<span class="cpp-keyword">void</span> pause()
{
    <span class="cpp-keyword">if</span>(kbhit())
    {
        <span class="cpp-keyword">while</span>(kbhit())
        {
        }
    }
    getch();
}

<span class="cpp-keyword">int</span> getInput(string output, <span class="cpp-keyword">int</span> min, <span class="cpp-keyword">int</span> max)
{
    string stringChoice;
    <span class="cpp-keyword">int</span> choice = <span class="cpp-number">0</span>;
    cout &lt;&lt; output + <span class="cpp-literal">" "</span> &lt;&lt; endl;
    <span class="cpp-keyword">while</span>(choice &lt; min || choice &gt; max)
    {
        cin &gt;&gt; stringChoice;
        choice = atoi(stringChoice.c_str());
        <span class="cpp-keyword">if</span>(choice &lt; min || choice &gt; max)
        {
            cout &lt;&lt; <span class="cpp-literal">"\nInvalid input.\n"</span> &lt;&lt; endl;
            cout &lt;&lt; output &lt;&lt; endl;
        }
        <span class="cpp-keyword">return</span> choice;
    }
}





</pre></div><!–ENDSCRIPT–><b>extra.h</b><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-comment">//extra.h</span>
<span class="cpp-comment">//Header file to define extra functions</span>

<span class="cpp-directive">#include</span> &lt;string&gt;
<span class="cpp-directive">#ifndef</span> EXTRA_H
<span class="cpp-directive">#define</span> EXTRA_H
<span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> std;

<span class="cpp-keyword">void</span> pause();
<span class="cpp-keyword">int</span> getInput(string output, <span class="cpp-keyword">int</span> min, <span class="cpp-keyword">int</span> max);
                         
<span class="cpp-directive">#endif</span>

</pre></div><!–ENDSCRIPT–>

Can someone tell me what the problem is here?

<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - Hrothgar15 on July 20, 2005 2:52:51 PM]<!–EDIT–></span><!–/EDIT–>
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...
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
What exactly should my includes be? I'm new to classes and objects and I figured this woudl be a good start...
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.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Whoops, I accidentally put the wrong code down for one of my files in the post. I fixed it.
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.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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?
I can see why you included board.h in cursor.h, but why did you include cursor.h in board.h?
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;
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++

This topic is closed to new replies.

Advertisement