Undefined reference for TicTacToe [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Using G++ to compile multiple .cpp and .h files
(13 answers)
Closed last year.
I've got this file where it seems to be an undefined reference but I don't know why, everything it seems correct to me. Someone can help me?
This is the error:
g++ -c driver.cpp -std=c++11 -pedantic -Wall
g++ -o driver driver.o
/usr/bin/ld: driver.o: in function `main':
driver.cpp:(.text+0x23): undefined reference to `TicTacToe::TicTacToe()'
/usr/bin/ld: driver.cpp:(.text+0x2f): undefined reference to `TicTacToe::makeMove()'
collect2: error: ld returned 1 exit status
make: *** [makefile:2: driver] Error 1
And these are the files I'm using:
The driver program:
// driver.cpp: use of TicTacToe class
#include "TicTacToe.h" // include definition of class TicTacToe
int main() {
TicTacToe g; // creates object g of class TicTacToe
g.makeMove(); // invokes function makeMove
}
This is the class:
// TicTacToe.h
#ifndef TICTACTOE_H
#define TICTACTOE_H
#include <array>
class TicTacToe {
private:
enum Status {WIN, DRAW, CONTINUE}; // enumeration constants
std::array<std::array<int, 3>, 3> board;
public:
TicTacToe(); // default constructor
void makeMove(); // make move
void printBoard() const; // print board
bool validMove(int, int) const; // validate move
bool xoMove(int); // x o move
Status gameStatus() const; // game status
};
#endif
These are the class function members:
// TicTacToe.cpp
// Member-function defnitions for class TicTacToe.
#include <iostream>
#include <iomanip>
#include "TicTacToe.h" // include definition of class TicTacToe
using std::cout;
using std::cin;
using std::setw;
TicTacToe::TicTacToe() {
for (int j{0}; j < 3; ++j) { // initialize board
for (int k{0}; k < 3; ++k) {
board[j][k] = ' ';
}
}
}
void TicTacToe::makeMove() {
printBoard();
while (true) {
if (xoMove('X')) {
break;
}
else if (xoMove('O')) {
break;
}
}
}
void TicTacToe::printBoard() const {
cout << " 0 1 2\n\n";
for (int r{0}; r < 3; ++r) {
cout << r;
for (int c = 0; c < 3; ++r) {
cout << setw(3) << static_cast< char > (board[r][c]);
if (c != 2) {
cout << " |";
}
}
if (r != 2) {
cout << "\n ____|____|____\n | | \n";
}
}
cout << "\n\n";
}
bool TicTacToe::xoMove(int symbol) {
int x;
int y;
do {
cout << "Player " << static_cast<char>(symbol) << " enter move: ";
cin >> x >> y;
cout << '\n';
} while (!validMove(x, y));
board[x][y] = symbol;
printBoard();
Status xoStatus = gameStatus();
if (xoStatus == WIN) {
cout << "Player " << static_cast<char>(symbol) << " wins!\n";
return true;
}
else if (xoStatus == DRAW) {
cout << "Game is draw.\n";
return true;
}
else { // CONTINUE
return false;
}
}
bool TicTacToe::validMove(int r, int c) const {
return r >= 0 && r < 3 && c >= 0 && c < 3 && board[r][c] == ' ';
}
// must specify that type Status is part of the TicTacToe class.
TicTacToe::Status TicTacToe::gameStatus() const {
// check for a win on diagonals
if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
return WIN;
}
else if (board[2][0] != ' ' && board[2][0] == board[1][1] && board[2][0] == board[0][2]) {
return WIN;
}
// check for win in rows
for (int a{0}; a < 3; ++a) {
if (board[a][0] != ' ' && board[a][0] == board[a][1] && board[a][0] == board[a][2]) {
return WIN;
}
}
// check for win in columns
for (int a{0}; a < 3; ++a) {
if (board[0][a] != ' ' && board[0][a] == board[1][a] && board[0][a] == board[2][a]) {
return WIN;
}
}
// check for a completed game
for (int r{0}; r < 3; ++r) {
for (int c{0}; c < 3; ++c) {
if (board[r][c] == ' ') {
return CONTINUE; // game is not finished
}
}
}
return DRAW; // game is a draw
}
It's probably something stupid but I don't know what I have to look for.

step by step:
g++ -c driver.cpp TicTacToe.cpp -std=c++11 -pedantic -Wall
g++ -o driver driver.o TicTacToe.o
./driver

Related

Tic Tac Toe C++ Check & Print Winner

have a problem with this coding, how to check and display the winner? I have tried to add it but turns into error due to the function checkWinner().
#include <iostream>
using namespace std;
void showBoard(void);
void playerInput(int p);
void checkWinner();
void nextPlayer(int);
int board[3][3]={{0,0,0},{0,0,0},{0,0,0}};
int main()
{
int r;
int c;
int player;
int winner;
int turns;
cout << "******* Tic Tac Toe Game *******" << endl;
showBoard();
nextPlayer(1);
checkWinner();
return 0;
}
Output board function:
void showBoard(void)
{
int r;
int c;
for(r=0; r<=2; r++)
{
for(c=0; c<=2; c++)
{
if( board [r][c]==0)
cout << "0 ";
else if (board [r][c]==1)
cout << "1 ";
else
cout << "2 ";
}
cout << endl;
}
}
This is the player input function:
void playerInput(int p)
{
int row;
int col;
if(p==1)
cout <<"You are player number 1 \n\n";
else
cout <<"You are player number 2 \n\n";
cout<<"Please enter your coordinate:";
cin>>row;
cout<<"\n";
cin>>col;
if(p==1)
board[--row][--col]=1;
if(p==2)
board[--row][--col]=2;
}
Heres the problem I'm facing now, how to make it display the winner?
void checkWinner()
{
int winner;
for (int i=0; i<=2; i++)
{
if(board[i][0]==board[i][1] && board[i][1]==board[i][2] && board[i][0]!=0)
{
winner=board[i][0];
}
}
for(int i=0; i<=2; i++)
{
if (board[0][i]==board[1][i] && board[1][i]==board[2][i] && board[0][i]!=0)
{
winner=board[0][i];
}
}
if(board[0][0]=board[1][1] && board[1][1]==board[2][2] && board[0][0]!=0)
{
winner=board[0][0];
}
if(board[0][2]=board[1][1] && board[1][1]==board[2][0] && board [0][2]!=0)
{
winner=board[0][2];
}
if(board[0][0]==board[0][1] && board[0][1]==board[0][2]&& board[0][2]==board[0][1]&& board[1][0]==board [1][1]&& board[1][1]==board [1][2]&& board[1][2]==board[2][0]&&board[2][0]==board [2][1]&& board[2][1]==board [2][2] && board [0][0]!=0)
{
winner=0;
}
}
void nextPlayer(int player)
{
playerInput(player);
showBoard();
if(player==1)
player=2;
else
player=1;
nextPlayer(player);
}
The problem is the game is not ended but it still asking for the player to resume and it keep updating the value.
click below to see the problem I'm facing. Thanks!
Click to view my output, it doesn't end the game
Your code has a serious recursion problem. The function checkWinner() in main never gets called because the function nextPlayer(1); is infinitely recursive, since, it lacks a base case.
A recursive base case, is what causes a recursive function to stop its recursion.
Note that in your code:
void nextPlayer(int player)
{
playerInput(player);
showBoard();
if(player==1)
player=2;
else
player=1;
nextPlayer(player);
}
once nextPlayer(1); is called from main, it never returns, because, it always enters into another call of its own nextPlayer(player); at the end of the function. For this you need a base case to stop this recursion at some point from happening.
Now, logically your base case should actually be, checking for a winner before proceeding to play, which is what is handled by the checkWinner() function.
But, the problem is that the checkWinner() function returns void which means nothing so, in its current state, we cannot use it as a base case.
However, if we redesign it to return something such as:
0 for play on
1 or 2 indicating a player has won
and lastly -1 indicating that the board is full and nobody won (a tie).
So, reforming the checkWinner() function as (I also corrected a few conditions so have a close look at the code):
int checkWinner()
{
int winner;
// any of the rows is same
for (int i=0; i<=2; i++)
{
if(board[i][0]==board[i][1] && board[i][1]==board[i][2] && board[i][0]!=0)
{
winner = board[i][0];
return winner;
}
}
// any of the columns is same
for(int i=0; i<=2; i++)
{
if (board[0][i]==board[1][i] && board[1][i]==board[2][i] && board[0][i]!=0)
{
winner = board[0][i];
return winner;
}
}
// 1st diagonal is same
if(board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[0][0]!=0)
{
winner = board[0][0];
return winner;
}
// 2nd diagonal is same
if(board[0][2]==board[1][1] && board[1][1]==board[2][0] && board [0][2]!=0)
{
winner = board[0][2];
return winner;
}
// if we reached here nobody has won yet
// if any empty box on board then play on
for(int i=0; i<=2; i++)
{
for(int j=0; j<=2; j++)
{
if(board[i][j]==0)
{
winner = 0;
return winner;
}
}
}
winner = -1; // all boxes full and nobody won so A tie has occurred
return winner;
}
Now, that it can be used, we will use it in void nextPlayer(int player) function as:
void nextPlayer(int player)
{
int winner = checkWinner();
if( winner == 0) // play on
{
playerInput(player);
showBoard();
if(player==1)
player=2;
else
player=1;
nextPlayer(player);
}
else if(winner == -1)
{
cout<<"\nGame drawn!\n";
}
else
{
cout<<"\nPlayer "<<winner<<" wins!\n"<<endl;
}
}
Note that, the checkWinner() condition is checked before we start to do anything.
And now, the call to checkWinner() in the main function, which was unreachable anyways, is not needed and will now be done before proceeding at every step in void nextPlayer(int player):
#include <iostream>
using namespace std;
void showBoard(void);
void playerInput(int p);
int checkWinner();
void nextPlayer(int);
int board[3][3]={{0,0,0},{0,0,0},{0,0,0}};
int main()
{
int r;
int c;
int player;
int winner;
int turns;
cout << "******* Tic Tac Toe Game *******" << endl;
showBoard();
nextPlayer(1);
return 0;
}
The rest of the functions are correct and will remain the same.
At two points in your code, you have used = in your if statement. This leads to initialization instead of equality checking. You should use == instead of the =.
if (board[0][0] = board[1][1] && /** This should be a == for checking equality */
board[1][1] == board[2][2] &&
board[0][0] != 0)
{
winner=board[0][0];
}
if (board[0][2] = board[1][1] && /** This should be a == for checking equality */
board[1][1] == board[2][0] &&
board[0][2] != 0)
{
winner = board[0][2];
}

array of strings inside the object

I've got a problem with creating an array of strings inside the object. I don't know how to patch it around so I'm asking for help. Here lies the problem:
main.h:
#pragma once
#include <iostream>
#include <conio.h>
#include <string>
class tab2D {
protected:
int width;
int height;
string **sTab;
int **iTab;
public:
tab2D();
tab2D(int x, int y, char c);
~tab2D();
tab2D(tab2D&t);
};
class chess: public tab2D {
public:
chess(int x, int y);
~chess();
chess(chess&c);
void init();
bool ifMove();
void show();
};
class matrix: public tab2D {
public:
matrix(int x, int y);
~matrix();
matrix(matrix&m);
};
The compiler says: syntax error : missing ';' before '*' about the line
string **sTab;
I assume that I can't make the dynamic array of strings and it makes further problems with processing this array.. Can you help me? :)
*UPDATE 1*Thanks, I forgot to add line
using namespace std;
Now it works, but I've got another problem.
#include "main.h"
using namespace std;
////// Konstruktor, konstruktor kopiujący oraz destruktor //////
chess::chess(int x = 8, int y = 8) : tab2D(x, y, 'c') {
init();
};
chess::chess(chess&c) {
chess(c.width, c.height);
};
chess::~chess() {
};
////// Metody //////
////// Uzupełnianie kolorów pól oraz rozstawianie figur i pionków //////
void chess::init() {
/// kolory pól: 0 - biały, 1 - czarny///
int last = 0;
for(int i = 0; i < this->height; ++i) {
for(int j=0; j < this->width; ++j) {
if(last = 0) {
this->sTab[i][j] = "1";
last = 1;
}
else if(last = 1) {
this->sTab[i][j] = "0";
last = 0;
}
}
if(last = 0)
last = 1;
else if(last = 1)
last = 0;
};
/// rozstawienie pionków ///
for(int i = 0; i < this->width; ++i) {
sTab[1][i] = sTab[1][i] + "0";
sTab[6][i] = sTab[6][i] + "a";
};
};
////// Wyświetlenie szachownicy //////
void chess::show() {
for(int i = 0; i < (this->height + 1); ++i) {
for(int j=0; j < (this->width + 1); ++j) {
if(i == 0 && j == 0)
cout << " ";
else if (i != 0 && j == 0) {
switch (i) {
case 1:
cout << "A ";
break;
case 2:
cout << "B ";
break;
case 3:
cout << "C ";
break;
case 4:
cout << "D ";
break;
case 5:
cout << "E ";
break;
case 6:
cout << "F ";
break;
case 7:
cout << "G ";
break;
case 8:
cout << "H ";
break;
default:
break;
}
}
else if (i == 0 && j != 0) {
cout << j << " ";
}
else {
cout << this->sTab[i-1][j-1] << " ";
}
}
cout << endl;
};
};
When I run the program, there is a breakpoint in the line
this->sTab[i][j] = "0";
I assume there is something wrong with making the array of strings but I don't understand why exactly there is a breakpoint and can't debug it.
UPDATE 2
Here is the code for tab.cpp:
#include "main.h"
using namespace std;
////// Konstruktor domyślny, konstruktor, konstruktor kopiujący oraz destruktor //////
tab2D::tab2D() {
};
tab2D::tab2D(int x, int y, char c) {
this->width = x;
this->height = y;
if (c == 'm') {
this->iTab = new int*[this->width];
for(int i=0;i<this->height;++i)
this->iTab[i] = new int[this->width];
}
else if (c == 'c') {
this->sTab = new string*[this->width];
for(int i=0;i<this->height;++i)
this->sTab[i] = new string[this->width];
}
else {
}
};
tab2D::tab2D(tab2D&t) {
tab2D(t.width, t.height, 't');
};
tab2D::~tab2D() {
for(int i=0;i<height;++i)
delete [] iTab[i];
delete [] iTab;
for(int i=0;i<height;++i)
delete [] sTab[i];
delete [] sTab;
};
You need to qualify names from the standard library:
std::string **sTab;
^^^^^
If you're doing what I think you're doing and allocating things with new, then you should consider using std::vector to deal with the quagmire of memory management issues you're about to encounter. If you really want to juggle pointers yourself for some reason, don't forget the Rule of Three.
UPDATE Your new problem might be because the copy constructor is horribly broken:
chess::chess(chess&c) {
chess(c.width, c.height);
};
This creates and destroys a temporary object, but doesn't initialise the object being constructed, leaving it in an invalid state. You probably don't want to declare a copy-constructor at all, as long as the base class is correctly copyable. If you did need one, it should should be more like:
chess::chess(chess const & c) : // 'const' so constant objects can be copied
tab2D(c) // copy the base-class subobject
{
// do whatever else needs doing
}
Alternatively, the new problem might be due to errors in the tab2D constuctors which you haven't shown us. The best way to track it down is to step through the program with a debugger, checking that everything is correctly initialised before use.
UPDATE Probably, the runtime error is caused by allocating the wrong number of pointers. You want
iTab = new int*[height]; // Not width
and likewise for sTab.

Really frustrating debug Error with C++

So I am trying to make a pretty basic TicTacToe in C++, and while I have no apparent syntax errors, I am having a lot of Debug errors of : "Unhandled exception at 0x0100142D in Cobra.exe: 0xC0000005: Access violation reading location 0xCCCCC359"
I feel like it is an obvious Error that I am just not processing but it;s definitely starting to grate my nerves. I'll label where the access error is... Right now it;s in my checkwin method but I feel like there is definitely more than one..
In my header I use a private char** board and a private int player.
#include "TicTacToe.h"
#include <iostream>
using namespace std;
int main()
{
int rowChosen,
colChosen;
TicTacToe newGame;
while(newGame.checkWin()==' ' && !newGame.fullBoard())
{
newGame.displayBoard();
do
{
cout << "Player " << newGame.getPlayer() << " choose a row and column.";
cin >> rowChosen >> colChosen;
newGame.setGame(rowChosen,colChosen);
}while(newGame.setGame(rowChosen,colChosen)==false);
newGame.makeMove(rowChosen, colChosen, newGame.getPlayer());
newGame.switchPlayer();
}
newGame.displayBoard();
if(newGame.checkWin()!=' ')
cout << "Player " << newGame.returnWinner() << " wins!";
else if(newGame.fullBoard()==true)
cout << "Cat's Game: This is a Draw!";
return 0;
}
TicTacToe::TicTacToe()
{
player = 1;
char blank = ' ';
for(int row=0;row<3;row++)
for(int col=0;col<3;col++)
board[row][col] = ' ';
}
void TicTacToe::setPlayer(int play)
{
player = play;
}
int TicTacToe::getPlayer()
{
return player;
}
void TicTacToe::switchPlayer()
{
if (player==1)
player++;
else
player--;
}
bool TicTacToe::setGame(int row, int col) //input validation
{
if (row >= 3 || row < 0)
return false;
if (col >= 3 || col < 0)
return false;
if (board[row][col] != ' ')
return false;
return true;
}
char TicTacToe::getBoard(int row, int col)
{
return board[row][col];
}
bool TicTacToe::fullBoard()
{
bool full = true;
for(int row=0;row<3;row++)
for(int col=0;col<3;col++)
{
if(board[row][col]==' ')
{
full=false;
break;
}
}
return full;
}
void TicTacToe::makeMove(int r, int c, int player)
{
char ch;
if (player==1)
ch = 'X';
else
ch = 'O';
board[r][c] = ch;
}
char TicTacToe::checkWin()
{
char b = ' ';
for(int i=0; i<3; i++) //horizontal
{
if((board[i][1]==board[i][0]) && (board[i][1]==board[i][2])) //THIS IS ERROR
{
b=board[i][1];
}
}
for(int j=0; j<3; j++) //vertical
{
if( (board[1][j]==board[0][j]) && (board[1][j]==board[2][j]) )
b=board[1][j];
}
if((board[0][0]==board[1][1] && board[1][1]==board[2][2]) ||
(board[2][0]==board[1][1] && board[1][1]==board[0][2]))
b= board[1][1];
return b;
}
void TicTacToe::displayBoard()
{
for(int row=0;row<3;row++)
{
cout << "|-----|";
for(int col=0;col<3;col++)
{
if(board[row][col]==' ')
cout << "| ";
else
cout << "|" << board [row][col];
}
cout << "|" << endl;
cout << "|-----|";
}
}
int TicTacToe::returnWinner()
{
int winner = 0;
if(checkWin()=='X')
winner = 1;
else if(checkWin()=='O')
winner = 2;
return winner;
}
This is my header TicTacToe.h
class TicTacToe
{
private:
char board[3][3]; //there we go
int player;
public:
TicTacToe();
void setPlayer(int);
int getPlayer();
void switchPlayer();
bool setGame(int,int);
char getBoard(int,int);
bool fullBoard();
void makeMove(int,int,int);
char checkWin();
void displayBoard();
int returnWinner();
};
Assuming you've included all the code, you're missing something essential: the declaration of the class TicTacToe. Of course, there's a TicTacToe.h, so it could be (probably is) hiding in there. That would help.
That all said, I think Mat has it right -- you declare a local named board inside the ctor and initialize it. But that goes out of scope as soon as the ctor is finished, which means all that initialization disappears into the aether.. Assuming you define board in your class, then simply removing that char declaration might be all you need.
Now, here's general advice: if you get an access violation exception, it usually means that you have made a mistake about the scope of something you're referring to. Since the exception happens at a point where you're using board, the slice of the program containing the identified board is the place to look.

Xcode error: "2 duplicate symbols for architecture x86_64"

I keep getting this error when I try to compile and have no idea why
Ld /Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Products/Debug/BATTLESHIP normal x86_64
cd /Users/Itunes/Desktop/Programs/CMPSC122/BATTLESHIP
setenv MACOSX_DEPLOYMENT_TARGET 10.7
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Products/Debug -F/Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Products/Debug -filelist /Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Intermediates/BATTLESHIP.build/Debug/BATTLESHIP.build/Objects-normal/x86_64/BATTLESHIP.LinkFileList -mmacosx-version-min=10.7 -stdlib=libc++ -o /Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Products/Debug/BATTLESHIP
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK5Point in:
/Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Intermediates/BATTLESHIP.build/Debug/BATTLESHIP.build/Objects-normal/x86_64/main.o
/Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Intermediates/BATTLESHIP.build/Debug/BATTLESHIP.build/Objects-normal/x86_64/Board.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK5Point in:
/Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Intermediates/BATTLESHIP.build/Debug/BATTLESHIP.build/Objects-normal/x86_64/main.o
/Users/Itunes/Library/Developer/Xcode/DerivedData/BATTLESHIP-gefqfgwzntvzlvfyyxoqzmmanjlb/Build/Intermediates/BATTLESHIP.build/Debug/BATTLESHIP.build/Objects-normal/x86_64/Ship.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
My headers and .cpp files are
Board.h
// Board.h - Board class declaration
#pragma once
#include "Point.h"
using namespace std;
#define HIT 'X'
#define SHIP 'S'
#define MISS 'O'
#define WATER '~'
class Board
{
public:
// Constructor
Board();
// Function which prints an image of the board showing hits ( X ) and misses ( O )
void PrintBoard();
// Resets the board for both players before new games begin
void ResetBoard();
// Marks both boards after each guess
void MarkBoard(bool hit, Point guessCoordinate);
// Returns TRUE if the user has guessed the given coordinate
bool CheckGuess( Point guessCoordinate );
private:
// Board used for determining whether the user has guessed a certain position yet or not
bool playerBoard[BOARD_WIDTH][BOARD_LENGTH] = {false};
// Board used soley for output
// KEY: 0 = not yet guessed, 1 = hit, 2 = miss
// intialize all spaces to 0 to show that no guesses have been made yet
int outputBoard[10][10] = { 0 };
};
Board.cpp
// Board.cpp - Board class function implementation
#include "Board.h"
Board::Board()
{
}
// Prints the output board to console
void Board::PrintBoard()
{
// Print column numbers
cout << " \t" << " " << 1;
for( int number = 2; number < 11; number++)
cout << " " << number;
cout << endl;
// Print board itself
// i = row number, j = column number
for( int i = 0; i < 10; i++)
{
// int i can double as a row and column counter for checking if a space has been hit
/*cout << " \t";
// Print row lines
for( int k = 1; k < 11; k++)
cout << " __";*/
cout << endl;
// Print row number
cout << i+1 << " ";
cout << "\t";
// Print columns
for( int j = 0; j < 10; j++)
{
cout << "|";
// INSERT CODE THAT PLACES X OR O IF GUESS IS HIT OR SHIP
if( outputBoard[i][j] == 0 )
cout << WATER;
if( outputBoard[i][j] == 1 )
cout << HIT;
if( outputBoard[i][j] == 2 )
cout << MISS;
}
cout << "|";
cout << endl;
}
cout << endl;
}
void Board::MarkBoard(bool hit, Point guessCoordinate)
{
// First mark the board to show the player has guessed this coordinate
playerBoard[guessCoordinate.X][guessCoordinate.Y] = true;
if( hit == true )
{
// Show that the player hit a ship
outputBoard[guessCoordinate.X][guessCoordinate.Y] = 1;
}
else
{
// Show that the player missed the ship
outputBoard[guessCoordinate.X][guessCoordinate.Y] = 2;
}
}
bool Board::CheckGuess(Point guessCordinate)
{
bool previouslyGuessed = false;
// If the given point has already been guessed, make the function return true
if( playerBoard[guessCordinate.X][guessCordinate.Y] )
{
previouslyGuessed = true;
}
else
{
previouslyGuessed = false;
}
return previouslyGuessed;
}
Ship.h
#pragma once
#include <string>
#include "Point.h"
using namespace std;
#define SHIP_TYPES 5
#define MAX_LENGTH 5
enum DIRECTION {HORIZONTAL,VERTICAL};
class Ship
{
public:
// Constructor
Ship();
// Function to initially set all ship to the correct values
void LoadShips( Ship ship[SHIP_TYPES] );
// Function which gives ships their location points and placement orientation
void FillShipData( DIRECTION direction, Point coordinate );
bool isSunk();
bool isHit( Point guess, Ship ship[SHIP_TYPES] );
string GetName()
{ return Name; }
private:
// Ship name
string Name;
// How many spaces the ship occupies
int Length;
// Determines whether a ship lies vertically or horizontally on the board
DIRECTION Direction;
// Holds the coordinates that the ship occupies
Point spaceOccupied[MAX_LENGTH];
};
Ship.cpp
#include "Ship.h"
Ship::Ship()
{
}
void Ship::LoadShips(Ship ship[SHIP_TYPES])
{
//Sets the default data for the ships
//we plan to include in the game
//IMPORTANT!! > MUST MATCH SHIP_TYPES -Default=5 (0-4)
ship[0].Name = "Cruiser"; ship[0].Length = 2;
ship[1].Name = "Frigate"; ship[1].Length = 3;
ship[2].Name = "Submarine"; ship[2].Length = 3;
ship[3].Name = "Escort"; ship[3].Length = 4;
ship[4].Name = "Battleship"; ship[4].Length = 5;
}
void Ship::FillShipData( DIRECTION direction, Point coordinate )
{
Direction = direction;
int x,y;
// If the ship will be laid horizontally, give it the initial point plus the x-coordinates to its right
if( Direction == HORIZONTAL )
{
while( coordinate.X+Length >= BOARD_WIDTH || coordinate.X < 0 || coordinate.Y < 0 || coordinate.Y >= BOARD_LENGTH )
{
cout << "Your ship will not be entirely on the board when placed this way. Please choose a new row number and column number separated by a space (ex: x y): ";
cin >> x >> y;
coordinate.X = x;
coordinate.Y = y;
}
for( int i = 0; i < Length; i++ )
{
spaceOccupied[i] = coordinate;
coordinate.X++;
}
}
// If the ship will be laid vertically, give it the initial point plus the y-coordinates below it
else
{
// Be sure the give point will contain and the ship
while( coordinate.Y+Length >= BOARD_LENGTH || coordinate.X < 0 || coordinate.Y < 0 || coordinate.X >= BOARD_WIDTH )
{
cout << "Your ship will not be entirely on the board when placed this way. Please choose a new row number and column number separated by a space (ex: x y): ";
cin >> x >> y;
coordinate.X = x;
coordinate.Y = y;
}
for( int i = 0; i < Length; i++ )
{
spaceOccupied[i] = coordinate;
coordinate.Y++;
}
}
}
// use throwaway to call, one user's guess and the other user's ships
//
bool Ship::isHit( Point guess, Ship ship[SHIP_TYPES] )
{
bool hit = false;
// For loop to examine each ship
for( int i = 0; i < SHIP_TYPES && hit==false; i++ )
{
// For loop to go through each point the ship occupies
for( int j = 0; j < ship[i].Length && hit==false; j++ )
{
// If the player's guess matches a point the ship is located on, it has been hit
if( ship[i].spaceOccupied[j] == guess )
{
hit = true;
// Inform player they landed a hit
cout << "Ship: " << ship[i].Name << " has been hit!\n";
}
}
}
// If the player has missed all ships, inform them
if( hit == false )
cout << "Missed.\n";
return hit;
}
And finally Point.h
#pragma once
#include <iostream>
using namespace std;
#define BOARD_LENGTH 10
#define BOARD_WIDTH 10
struct Point
{
// A location on the grid defined
// by X(horizontal) Y(vertical) coordinates
int X;
int Y;
bool operator == (const Point& compareTo) const
{
return ( (X == compareTo.X) && (Y == compareTo.Y) );
}
};
ostream& operator <<(ostream& out, const Point& myPoint)
{
out << "(" << myPoint.X << "," << myPoint.Y << ")";
return out;
}
My main function simply includes Board.h and Ship.h with no function declarations.
Any help would be greatly appreciated!
Stupid one, but make sure you haven't #imported a .m file by mistake somewhere
The problem is that you are redefining the operator<<(ostream&, const Point&) in main.cpp Board.cpp, and Ship.cpp by including Point.h in each.
You can most easily solve this problem by making it inline, in Point.h.
inline ostream& operator <<(ostream& out, const Point& myPoint) // << add inline
When you see those strange symbol names in the error output, a trick is to use c++filt in your Terminal to see more easily what the function name or symbol is that causing the problem. This "unmangles" the symbol name, for example, try this in Terminal:
phillip / $ c++filt __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK5Point
operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Point const&)
phillip / $
add inline here.
inline ostream& operator <<(ostream& out, const Point& myPoint)
^^^^^^
{
out << "(" << myPoint.X << "," << myPoint.Y << ")";
return out;
}
Chances are that you have object files (.o) in your Xcode project. Xcode doesn't like this. Remove the object files and the code should compile.

Undefined symbols for architecture x86_64 reference linker error

So I've run into this compiling error when I put the following:
bash-3.2$ g++ -o a *.cpp
Undefined symbols for architecture x86_64:
"set_function::set_function(int)", referenced from:
_main in ccezs7Gk.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
but it seems as though everything is right in my files as far as referencing. Maybe I'm missing something?
//
// hw1_sets.cpp
//
//
#include <iostream>
#include <vector>
#include "hw1.h"
using namespace std;
void set_function::assign() //Assign function
{
cin >> set_function::sets;
cout << set_function::sets << endl;
int i;
if(sets == "R") //if user inputs R
{
for(i=0; i<13; i++) //for loop inputting number into R set
{
cin >> R[i];
// return R[i];
}
}
else if (sets == "S") //if user inputs S
{
for(i=0; i<13; i++) //for loop inputting number into S set
{
cin >> S[i];
// return S[i];
}
}
else if (sets == "T") //if user inputs T
{
for(i=0; i<13; i++) //for loop inputting number into T set
{
cin >> T[i];
// return T[i];
}
}
else
{
cout << "This set does not exist! Try again!" << endl;
}
cout << " set complete" << endl;
};
void set_function::clear() //Clear function
{
//find set
/*cin >> set_function::set;
cout << set_function::set << endl;
int i;
if(set == set_function::R)
{
for(i=0; i<13; i++)
{
//clear R values
}
}
else if (set == set_function.S)
{
for(i=0; i<13; i++)
{
//clear S values
}
}
else if (set == T)
{
for(i=0; i<13; i++)
{
//clear T values
}
}
//remove all values*/
}
void set_function::unionn() //Union function
{
//for loop from 0 to 12 (through all elements)
//if set1[x] or set2[x] = 1
//solution[x]=1
//else
//solution[x]=0
}
void set_function::intersection() //Intersection function
{
//for loop from 0 to 12 (through all elements)
//if set1[x] == set2[x]
//solution[x]=set1[x]
//else
//solution[x]=0
}
void set_function::difference() //difference function
{
//for loop from 0 to 12 (through all elements)
//set1[x]-set2[x]=solution[x]
}
/*printing the set doesn't work*/
void set_function::print() //print function
{
/*string setname;
cin >> setname;
if (setname = "R")
{
for(int i = 0; i<13; i++)
{
cout<< R[i] << "-";
}
cout << endl;
}
else if (setname = "S")
{
for(int i = 0; i<13; i++)
{
cout<< S[i] << "-";
}
cout << endl;
}
else if (setname = "T")
{
for(int i = 0; i<13; i++)
{
cout<< T[i] << "-";
}
cout << endl;
}
//else if lastdigit
//end of command or newline*/
}
//exit
//
// hw1.cpp
//
//
//
//
#include "hw1.h"
#include <iostream>
using namespace std;
int main()
{
string function;
set_function sets(27);
while(1)
{
cout << "sets> ";
cin >> function;
if(function=="assign")
{
sets->assign();
}
else if(function=="clear")
{
sets->clear();
}
else if(function=="union")
{
sets->unionn();
}
else if(function=="intersection")
{
sets->intersection();
}
else if(function=="difference")
{
sets->difference();
}
else if(function=="print")
{
sets->print();
}
else if(function=="quit")
{
// sets->quit();
return 0;
}
else
{
cout<<"error"<<endl;
}
}
}
//
// hw1.h
//
//
//
//
#include <iostream>
using namespace std;
class set_function
{
private:
bool R[13];
bool S[13];
bool T[13];
public:
string sets;
int values[13];
/*void r()
{
R.resize(13);
}
void s()
{
S.resize(13);
}
void t()
{
T.resize(13);
}*/
set_function(int a){}
void assign();
void clear();
void unionn();
void intersection();
void difference();
void print();
// void quit();
};
edit (10/3/13 #12:50p): I changed what was commented on and now I get this problem:
hw1.cpp: In function ‘int main()’:
hw1.cpp:28: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:32: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:36: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:40: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:44: error: base operand of ‘->’ has non-pointer type ‘set_function’
hw1.cpp:48: error: base operand of ‘->’ has non-pointer type ‘set_function’
edit (10/3/13 #1:23p): Fixed. changed the following:
set_function *sets = new set_function(27)
to
set_function *sets;
sets = new set_function(27);
compiles correctly now. Thanks!
That's not a compiler error, it's a linker error. You're getting it because you declared a constructor for your set_function class but didn't ever define it.
So you declare set_function::set_function(int a) in hw1.h. Then main.cpp is correctly compiled as the call to the constructor of set_function is correct as found declared in the header file hw1.h.
But that function is not implemented anywhere, linking occurs and the call is not resolved.
You should implement it right in the header file or in hw1.cpp file.