IFeel3 Temat założony przez niniejszego użytkownika |
Problem w kompilacji - undefined reference to... » 2014-02-21 23:52:46 Witam. Ucząc się podstaw programowania korzystałem z e-booka "Od zera do gier kodera", gdzie po starannym (prze)pisaniu prostego programu na grę w kółko i krzyżyk natknąłem się na nieprzewidziany błąd w kompilacji: -------------- Build: Debug in Kolko i krzyzyk (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -o "bin\Debug\Kolko i krzyzyk.exe" obj\Debug\main.o obj\Debug\main.o: In function `main': C:/Documents and Settings/.../Kolko i krzyzyk/main.cpp:8: undefined reference to `StartGry()' C:/Documents and Settings/.../Kolko i krzyzyk/main.cpp:12: undefined reference to `RysujPlansze()' C:/Documents and Settings/.../Kolko i krzyzyk/main.cpp:14: undefined reference to `g_StanGry' C:/Documents and Settings/.../Kolko i krzyzyk/main.cpp:18: undefined reference to `Ruch(unsigned int)' C:/Documents and Settings/.../Kolko i krzyzyk/main.cpp:20: undefined reference to `g_StanGry' C:/Documents and Settings/.../Kolko i krzyzyk/main.cpp:20: undefined reference to `g_StanGry' collect2.exe: error: ld returned 1 exit status Process terminated with status 1 (0 minutes, 5 seconds) 6 errors, 0 warnings (0 minutes, 5 seconds)
Pliki zawarte w projekcie mają następującą treść: main.cpp #include <iostream> #include <conio.h> #include "game.h"
int main() { StartGry(); for(;; ) { RysujPlansze(); if( g_StanGry == GS_MOVE ) { unsigned uNumerPola; std::cin >> uNumerPola; Ruch( uNumerPola ); } else if( g_StanGry == GS_WON or g_StanGry == GS_DRAW ) break; } return 0; getch(); }
game.cpp #include <iostream> #include <ctime> #include "game.h"
FIELD g_aPlansza[ 3 ][ 3 ] = { { FLD_EMPTY, FLD_EMPTY, FLD_EMPTY }, { FLD_EMPTY, FLD_EMPTY, FLD_EMPTY }, { FLD_EMPTY, FLD_EMPTY, FLD_EMPTY } }; GAMESTATE g_StanGry = GS_NOTSTARTED; SIGN g_AktualnyGracz;
StartGry() { if( g_StanGry != GS_NOTSTARTED ) return false; srand( time( 0 ) ); g_AktualnyGracz =( rand % 2 == 0 ? SGN_CIRCLE: SGN_CROSS ); g_StanGry = GS_MOVE; return true; }
Ruch( unsigned uNumerPola ) { if( g_StanGry != GS_MOVE ) return false; if( !( uNumerPola >= 1 && uNumerPola <= 9 ) ) return false; unsigned uY =( uNumerPola - 1 ) / 3; unsigned uX =( uNumerPola - 1 ) % 3; if( g_aPlansza[ uY ][ uX ] == FLD_EMPTY ) g_aPlansza[ uY ][ uX ] = static_cast < FIELD >( g_AktualnyGracz ); const LINIE[ 8 ][ 3 ][ 2 ] = { { { 0, 0 }, { 0, 1 }, { 0, 2 } }, { { 1, 0 }, { 1, 1 }, { 1, 2 } }, { { 2, 0 }, { 2, 1 }, { 2, 2 } }, { { 0, 0 }, { 1, 0 }, { 2, 0 } }, { { 1, 0 }, { 1, 1 }, { 1, 2 } }, { { 2, 0 }, { 2, 1 }, { 2, 2 } }, { { 0, 0 }, { 1, 1 }, { 2, 2 } }, { { 0, 2 }, { 1, 1 }, { 2, 0 } } } FIELD Pole, ZgodnePole; unsigned uLiczbaZgodnychPol; for( int i = 0; i <= 7; i++ ) { Pole = ZgodnePole = FLD_EMPTY; uLiczbaZgodnychPol = 0; for( int j = 0; j <= 2; j++ ) { Pole = g_aPlansza[ LINIE[ i ][ j ][ 0 ] ][ LINIE[ i ][ j ][ 1 ] ]; if( Pole != ZgodnePole ) { ZgodnePole = Pole; uLiczbaZgodnychPol = 1; } else uLiczbaZgodnychPol++; } if( uLiczbaZgodnychPol == 3 && ZgodnePole != FLD_EMPTY ) { g_StanGry = GS_WON; return true; } } unsigned uLiczbaZapelnionychPol = 0; for( int i = 0; i <= 2; i++ ) for( int j = 0; j <= 2; j++ ) if( g_aPlansza[ i ][ j ] != FLD_EMPTY ) uLiczbaZapelnionychPol++; if( uLiczbaZapelnionychPol == 9 ) { g_StanGry = GS_DRAW; return true; } g_AktualnyGracz =( g_AktualnyGracz == SGN_CIRCLE ? SGN_CIRCLE: SGN_CROSS ) return true; }
RysujPlansze() { if( g_StanGry == GS_NOTSTARTED ) return false; system( "cls" ); std::cout << " KOLKO I KRZYZYK " << std::endl; std::cout << "---------------------" << std::endl; std::cout << std::endl; std::cout << " -----" << std::endl; for( int i = 0; i < 3; ++i ) { std::cout << " |"; for( int j = 0; j < 3; ++j ) { if( g_aPlansza[ i ][ j ] == FLD_EMPTY ) std::cout << i * 3 + j + 1; else std::cout << static_cast < char >( g_Plansza[ i ][ j ] ); } std::cout << "|" << std::endl; } std::cout << " -----" << std::endl; std::cout << std::endl; switch( g_StanGry ) { case GS_MOVE: std::cout << "Podaj nr pola, na ktorym " << std::endl; std::cout << "chcesz postawic " <<( g_AktualnyGracz == SGN_CIRCLE ? "kolko": "krzyzyk" ) << ": "; break; case GS_WON: std::cout << "Wygral gracz stawiajacy " <<( g_AktualnyGracz == SGN_CIRCLE ? "kolka": "krzyzyki" ); break; case GS_DRAW: std::cout << "Remis!"; break; } return true; }
game.h enum SIGN { SGN_CIRCLE = 'O', SGN_CROSS = 'X' };
enum GAMESTATE { GS_NOTSTARTED, GS_MOVE, GS_WON, GS_DRAW };
enum FIELD { FLD_EMPTY, FLD_CIRCLE = SGN_CIRCLE, FLD_CROSS = SGN_CROSS };
extern GAMESTATE g_StanGry;
bool StartGry();
bool Ruch( unsigned );
bool RysujPlansze();
Korzystam z CodeBlocks 12.11. Proszę o pomoc, jestem przekonany, że kod jest napisany zgodnie z poleceniami w "podręczniku". |