undefined reference to class function
hey, so I am working with two classes: a cn_board and a node for building a game tree called cn_board_node.
each class has 3 files: a header, an implementationa and a file called testing.cpp which I use to test functions as I write them.
however for some reason, one, and only one of my member functions is throwing a undefined reference error when used in my other class, despite working fine in its testing.cpp file.
here are the files:
cn_board.h
Code:
#ifndef CN_BOARD_H
#define CN_BOARD_H
#include <vector>
#include <iostream>
class cn_board
{
public:
cn_board() {}
cn_board(char width_in, char height_in, char win_length_in);
char get_width() { return width; }
char get_height() { return height; }
bool play_move(char column, char player);
void cout_board(); // for testing, shouldnt ever be used in final project
bool test_win(char x, char y);
protected:
private:
bool test_v(char x, char y);
bool test_h(char x, char y);
bool test_d(char x, char y);
std::vector<std::vector<char> > board;
char width;
char height;
char win_length;
char game_won;
};
#endif // CN_BOARD_H
cn_board.cpp
Code:
#include "cn_board.h"
cn_board::cn_board(char height_in, char width_in, char win_length_in)
{
height = height_in;
width = width_in;
win_length = win_length_in;
board.resize(width);
for(char c = 0; c < width; c++) // hehe
board[c].resize(height);
}
bool cn_board::play_move(char column, char player)
{
for(char h = 0; h < height; h++)
{
if(!board[column][h])
{
board[column][h] = player;
return 1;
}
}
return 0;
}
void cn_board::cout_board()
{
for(char y = height - 1; y >= 0; y--)
{
std::cout << "|";
for(char x = width - 1; x >= 0; x--)
{
std::cout << board[x][y] << "|";
}
std::cout << std::endl;
}
}
bool cn_board::test_win(char x, char y)
{
if(game_won)
return 1;
if(board[x][y])
{
if(test_v(x, y))
return 1;
if(test_h(x, y))
return 1;
if(test_d(x, y))
return 1;
}
return 0;
}
bool cn_board::test_v(char x, char y)
{
char line_length = 1;
for(char y_down = y - 1; y_down >= 0 && board[x][y] == board[x][y_down]; y_down--)
line_length++;
if(line_length >= win_length)
return 1;
return 0;
}
bool cn_board::test_h(char x, char y)
{
char line_length = 1;
for(char x_right = x + 1; x_right < width && board[x][y] == board[x_right][y]; x_right++)
line_length++;
for(char x_left = x - 1; x_left >= 0 && board[x][y] == board[x_left][y]; x_left--)
line_length++;
if(line_length >= win_length)
return 1;
return 0;
}
bool cn_board::test_d(char x, char y)
{
//test for diagonals with a positive slope
char line_length = 1;
char x_positive = x + 1;
char x_negative = x - 1;
char y_positive = y + 1;
char y_negative = y - 1;
for(; x_positive < width && y_positive < height && board[x][y] == board[x_positive][y_positive]; x_positive++ * y_positive++)
line_length++;
for(; x_negative >= 0 && y_negative >= 0 && board[x][y] == board[x_negative][y_negative]; x_negative-- * y_negative--)
line_length++;
if(line_length >= win_length)
return 1;
// now test for diagonals with a negative slope
line_length = 1;
x_positive = x + 1;
x_negative = x - 1;
y_positive = y + 1;
y_negative = y - 1;
for(; x_positive < width && y_negative >= 0 && board[x][y] == board[x_positive][y_negative]; x_positive++ * y_negative--)
line_length++;
for(; x_negative >= 0 && y_positive < height && board[x][y] == board[x_negative][y_positive]; x_negative-- * y_positive++)
line_length++;
if(line_length >= win_length)
return 1;
return 0;
}
testing.cpp //for cn_board
Code:
#include <iostream>
#include <vector>
#include "cn_board.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cn_board adsf(4,5,3);
adsf.play_move(1, 'X');
cn_board* adsf_ptr = &adsf;
adsf_ptr->play_move(2, '0');
adsf.cout_board();
adsf.test_win(1,3);
return 0;
}
cn_board_node.h
Code:
#ifndef CN_BOARD_NODE_H
#define CN_BOARD_NODE_H
#include "..\cn_board\cn_board.h"
class cn_board_node
{
public:
cn_board_node(char last_move_in); //for creating root nodes
char get_last_move() { return last_move; }
cn_board* get_board();
protected:
private:
cn_board_node(char last_move_in, cn_board_node &parent_in); //leaf nodes
char last_move;
char score; //out of 100
char player;
cn_board_node* children[];
cn_board_node* parent;
};
namespace cn_board_storage
{
extern cn_board* board; // holds original board, only used by root node
};
#endif // CN_BOARD_NODE_H
cn_board_node.cpp
Code:
#include "cn_board_node.h"
//for creating root nodes
cn_board_node::cn_board_node(char last_move_in)
{
last_move = last_move_in;
parent = NULL;
}
//for creating leaf/branch nodes
cn_board_node::cn_board_node(char last_move_in, cn_board_node &parent_in)
{
last_move = last_move_in;
parent = &parent_in;
}
cn_board* cn_board_node::get_board()
{
if(parent)
{
cn_board* temp = parent->get_board();
// getting an error here, cant figure out why:
temp->play_move(last_move, player);
return temp;
}
return cn_board_storage::board;
}
namespace cn_board_storage
{
cn_board* board;
}
testing.cpp //for cn_board_node
Code:
#include <iostream>
#include "cn_board_node.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
cn_board_node root(1);
return 0;
}
the problem is specifically in get_board():
Code:
//getting an error here, cant figure out why:
temp->play_move(last_move, player);
Am I including my classes incorrectly? or maybe it is something else?
BTW: The classes are each in their own folders, which are in the same directory. I am compiling with MinGW through Code::Blocks