undefined reference to class function

This is a discussion on undefined reference to class function within the C++ Programming forums, part of the General Programming Boards category; hey, so I am working with two classes: a cn_board and a node for building a game tree called cn_board_node. ...

  1. #1
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87

    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
    Last edited by bobknows; 11-27-2012 at 12:20 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Not what you asking : The constructor you call in main has a char as argument but you are passing an int as argument.

    edit : Are you sure that temp is not null?
    Last edited by std10093; 11-27-2012 at 12:27 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    last_move and player are the names of members of cn_board_node. In that line "temp->play_move(last_move, player);", temp is a pointer to cn_board, which does not have member named last_move or player.

    The compiler will therefore reject the code.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    are you saying, I have to call play_move with members of cn_board?

    If you will look in testing for cn_board, I call play_move with two literals, and it works fine, however, using two literals instead of variables in cn_board_node still gives an error
    Code:
        temp->play_move(1, '0'); //both still give the same error
        temp->play_move(temp->get_height(), temp->get_width());
    std: I am asking why my call to temp->play_move() doesn't work in cn_board_node, but is working fine in testing for cn_board
    Last edited by bobknows; 11-27-2012 at 04:01 PM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by bobknows View Post
    std: I am asking why my call to temp->play_move() doesn't work in cn_board_node, but is working fine in testing for cn_board
    Yes, you were clear. That's why i said not what you are asking. What I said is a general observation.

    But you did not answer if you are sure it is not null.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Could you post the error? The real message from your compiler or linker? I've read all that stuff and I'm still left guessing. Your compiler already knows damn well what went wrong and I don't feel like doing that work all over again.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    I was able to build an executable from the code after dealing with an issue where it didn't like the children data member in the cn_board_node class definition. When I removed the cn_node.cpp file from the project I set up and pressed the "rebuild all" option it complained with an undefined reference error to the play_move function. That could be the error you are getting. Are you sure that the source file is a part of your project? My project contained a main.cpp file (your testing.cpp), and the cn_board.cpp and cn_board_node.cpp files along with the two header files. If you do not include the cn_board.cpp source file directly in your project you must at least link to a previously compiled version of the source in your linker options.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    Including cn_board.cpp worked, I didn't try it before because other functions in it were working fine, which is weird.
    Thanks for the help.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Do not include .cpp files. Compile and link all .cpp files.
    Eg (g++): g++ a.cpp b.cpp c.cpp -o myexe.exe
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to (function)
    By django in forum C Programming
    Replies: 6
    Last Post: 09-04-2011, 12:06 PM
  2. undefined reference to a function
    By ChoCo in forum C Programming
    Replies: 1
    Last Post: 10-04-2009, 12:08 AM
  3. Undefined Reference to function?
    By fireonyx in forum C Programming
    Replies: 6
    Last Post: 03-17-2006, 05:15 PM
  4. undefined reference to a function
    By rodrigouy in forum C Programming
    Replies: 7
    Last Post: 01-17-2006, 05:47 AM
  5. undefined reference to function in class error
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 10-18-2005, 09:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21