Results 1 to 3 of 3

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User MrShadow's Avatar
    Join Date: Jul:2007
    Location:
    Posts: 2,606

    C++ Linker error - :)

    ,

    . main() . , .h .cpp main() :

    [Linker error] undefined reference to `TTT::TTT()'
    [Linker error] undefined reference to `move(int, TTT*)'
    ld returned 1 exit status

    devc++. 1 .


    :

    Code:
    #include <iostream>
    #include <cstdlib>
    #include "TTT.h"
    
    using namespace std;
    
    int main(){
        TTT* game = new TTT;
        int counter = 0;
        bool check = false;
        do{
           check = move(counter,game);
           counter++;
        }while(counter < 9 && check);
        
        system("PAUSE");
        return 0;
    }
    Code:
    #include <iostream>
    #include <cstdlib>
    
    const int SIZE = 3; // the size of the rows and columns used for the game
    
    using namespace std;
    
    TTT::TTT(){
               matrix = new int*[SIZE];
               for(int i = 0; i < SIZE; i++){
                       matrix[i] = new int[SIZE];
               }
               
               // setting the matrix to all 0. Will use the convension 1 means X, 2 means O, everything else means empty
               for(int i = 0; i < SIZE; i++){
                       for(int j = 0; j < SIZE; j++){
                               matrix[i][j] = 0;
                       }
               }
               print();
    }
    
    
    TTT::~TTT(){
                for(int i = 0; i < SIZE; i++){
                        delete matrix[i];
                }
                delete[] matrix;
    }
    
    void TTT::setPos(int x, int y, int value){
        matrix[x][y] = value;
    }
    
    bool TTT::isEmpty(int x, int y){
        if( matrix[x][y] == 0){
            return true;
        }
        return false;
    }
    
    
    void TTT::print(){
         cout<<endl;
         for(int i = 0; i < SIZE; i++){
                 for(int j = 0; j < SIZE; j++){
                         if( matrix[i][j] == 0 ){
                             cout << " ";
                             }
                         else if( matrix[i][j] == 1 ){
                              cout << "X";
                              }
                         else cout << "O";
                         if(j < SIZE - 1){
                              cout<< "|";
                         }
                 }
                 cout<< endl;
         }
    }
    
    int randRoll(){
        return rand()%3;
    }
    
    void TTT::compMove(){
         int x = randRoll();
         int y = randRoll();;
         if( isEmpty(x,y)) {
             matrix[x][y] = 2;
         }
         else{
              compMove();
         }
    }
    
    void TTT::humanMove(){
         int x,y;
         bool check = true;
         cout<<endl<<"Please enter coordinates(x and y, both between 1 and 3): ";
         do{
         cin>>x>>y;
         if(x <1 || x > 3 || y < 1 || y > 3){
              cout<<endl<<"One of the coordinates is out of the boundaries. Please enter new coordinates(x and y, both between 1 and 3): ";
         }
         else if( isEmpty(x-1,y-1) == false ){
              cout<<endl<<"This field is already filled. Please select a new one by giving new coordinates(x and y, both between 1 and 3): ";
              check = false;
         }
         else {
              matrix[x-1][y-1] = 1;
              check = true;
         }
         cin.ignore(numeric_limits<streamsize>::max(),'\n');
         cin.clear();
         }while(!check);
    }   
    
    
    int TTT::rowCheck(){
         for( int i = 0; i < SIZE; i++){
              if( matrix[i][0] != 0 ){
                  if( matrix[i][0] == matrix[i][1] && matrix[i][1] == matrix[i][2]){
    
                      return matrix[i][0];
                  }
              }
         }
         return 0;
    }
                   
                   
              
              
    int TTT::colCheck(){
         for( int i = 0; i < SIZE; i++){
              if( matrix[0][i] != 0 ){
                  if( matrix[0][i] == matrix[1][i] && matrix[1][i] == matrix[2][i]){
                      return matrix[0][i];
                  }
              }
         }
         return 0;
    }
    
    
    
    int TTT::diagCheck(){
        if( matrix[0][0] != 0 ){
            if ( matrix[0][0] == matrix[1][1] && matrix[1][1] == matrix[2][2] ){
                 return matrix[0][0];
            }
        }
        if( matrix[2][0] != 0 ){
            if ( matrix[2][0] == matrix[1][1] && matrix[1][1] == matrix[0][2] ){
                 return matrix[2][0];
            }
        }
        return 0;
    }
    
    
    
    
    int TTT::winCheck(){
        if( rowCheck() == 0 ){
            if( colCheck() == 0 ){
                if( diagCheck() == 0 ){
                    return 0;
                }
                else return diagCheck();
            }
            else return colCheck();
        }
        else return rowCheck();
    }
    
    
         
    bool move(int m, TTT* ttt){
         if( m%2 == 0 ){
             cout<<"Human move";
             ttt->humanMove();
         }
         else { 
              cout<<"Comp move";
              ttt->compMove();
         }
         ttt->print();
         
         int set = ttt->winCheck();
         
         if( set == 0 ) return true;
         else if( set == 1 ){
             cout<<endl<<"YOU WIN!"<<endl;
             return false;
         }
         else if( set == 2 ){
             cout<<endl<<"YOU LOST!"<<endl;
             return false;
         }
         
    }
    Code:
    #ifndef TTT
    #define TTT
    
    class TTT{
          public:
                 TTT();
                 ~TTT();
                 void setPos(int x, int y, int value);
                 bool isEmpty(int x, int y);
                 void print();
                 void compMove();
                 void humanMove();
                 int winCheck();
                 int rowCheck();
                 int colCheck();
                 int diagCheck();
          private:
                  int** matrix;
    };
    
    int randRoll();
    
    bool move(int m, TTT* ttt);
    
    #endif
    Edit: 1-2 . ,

    --------- 08:49 --------- : 08:08 ---------

    ( )

    "makefile" DevC++ "project" TTT.cpp main.cpp
    Last edited by MrShadow; 22nd June 2012 at 08:34.

  2. #2
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    , . , :

    1. .cpp ( )
    2. , . , , TTT.cpp . ( .h , TTT.cpp , - , , ). , , .
    3. . :

      .h :
      Code:
      bool parseCoords(const string& s, int& x1, int& y1, int& x2, int& y2);
      .cpp :
      Code:
      bool parseCoords(string& s, int& x1, int& y1, int& x2, int& y2)
      {
      ....
      }
      , - , const. ,
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  3. #3
    Registered User MrShadow's Avatar
    Join Date: Jul:2007
    Location:
    Posts: 2,606

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |