Haker123 Temat założony przez niniejszego użytkownika |
[C++] Błąd przy kompilacji ( error LNK2019 fatal error LNK1120) » 2014-02-20 19:50:25 Witam. Przy sprawdzaniu kodu dostalem 2 dziwne bledy których nie rozumiem. 1>Labirynt.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Labirynt::Wypisz_pliki(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,int &)" (?Wypisz_pliki@Labirynt@@QAE_NV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@AAH@Z) referenced in function "public: bool __thiscall Labirynt::EDITION(void)" (?EDITION@Labirynt@@QAE_NXZ) 1>D:\Programowanie\Projekty - Programy\Moje\MiX\Debug\MiX.exe : fatal error LNK1120: 1 unresolved externals
Błędy pojawily sie kiedy kombinowalem jak wyslac vector <string> (lub <char>) przez referencje do funkcji Wypisz_pliki, a kombinowalem dlatego ze w tej funkcji przy wpisywaniu danych do vektora wyskakiwaly mi bledy zlego typu (char do char[261]). W internecie znalazłem informacje że mogłem wybrać zły typ projektu a wybrałem pusty projekt. Korzystam z Microsoft Visual C++ 2010 Express. Plik Labirynt.h #ifndef LABIRYNT_H #define LABIRYNT_H
#include <string> #include <vector> #include <dirent.h>
using namespace std;
class Labirynt { public: enum MODE { M_EXIT, M_WIN, M_EDIT, M_GAME } TRYB; enum SINGS { GRACZ = '$', TLO = ' ', SCIANA1 = '|', SCIANA2 = '-', META = '@', EDIT = '#', CHECK = '*' }; Labirynt() : iLicznik( 0 ) , TRYB( M_EXIT ) { } bool Narysuj(); bool Ruch(); bool NEW(); bool EDITION(); bool CHECKS(); void DELET_MAP(); bool Wypisz_pliki( vector < string > SPIS, int & wyb ); private: struct Pozycja { int x; int y; } sGracz, sWskaznik, sMeta; int iLicznik; struct sDanePlanszy { char cNazwaPlanszy[ 100 ]; SINGS PLANSZA[ 20 ][ 20 ]; } sPlansza, sDodatkowa; bool NazwaPliku(); bool OtworzPlik(); }; #endif
Plik Labirynt.cpp #include <iostream> #include <string> #include <vector> #include <iomanip> #include <conio.h> #include <fstream> #include <ctime> #include <dirent.h> #include <cstdio> #include "Labirynt.h"
using namespace std;
string center( std::string input, int width ) { return std::string(( width - input.length() ) / 2, ' ' ) + input; }
bool zab_cin( int & wyb ) { do { cin.clear(); cin.sync(); cin >> wyb; if( cin.good() ) break; cout << "\nTO NIE CYFRA.\n\tNUMER: "; } while( true ); wyb--; return true; }
int Random( int nMin, int nMax ) { return rand() %( nMax - nMin + 1 ) + nMin; }
bool Labirynt::NazwaPliku() { srand( static_cast < unsigned int >( time( NULL ) ) ); vector < string > NazwyPlikow; DIR * pdir = NULL; struct dirent * ps_dirent = NULL; pdir = opendir( ".\\Maps" ); if( pdir ) { while( ps_dirent = readdir( pdir ) ) { if( !( strstr( ps_dirent->d_name, "adi" ) == NULL ) ) NazwyPlikow.push_back( ps_dirent->d_name ); if( NazwyPlikow.empty() ) { cout << "W katalogu \"Maps\" nie ma zadnych map.\n"; getch(); return false; } int i = Random( 0, NazwyPlikow.size() ); strcpy( sPlansza.cNazwaPlanszy, NazwyPlikow[ i ].c_str() ); } } else { cout << "\t Nie mozna otworzyc sciezki do Maps. Plik moze nie istniec lub byc poza glownym folderem.\n"; getch(); return false; } closedir( pdir ); return true; }
bool Labirynt::OtworzPlik() { string lokalizacja = "Maps\\"; lokalizacja = lokalizacja + sPlansza.cNazwaPlanszy; ifstream strumien( lokalizacja.c_str(), ios::in | ios::binary ); if( strumien ) { strumien.read( reinterpret_cast < char *>( & sPlansza ), sizeof( sPlansza ) ); } else { cout << "BLAD OTWARCIA PLIKU."; getch(); strumien.clear(); strumien.close(); return false; } for( int y = 0; y < 20; y++ ) for( int x = 0; x < 20; x++ ) { if( sPlansza.PLANSZA[ y ][ x ] == '@' ) { sMeta.y = y; sMeta.x = x; } if( sPlansza.PLANSZA[ y ][ x ] == '$' ) { sGracz.y = y; sGracz.x = x; } } strumien.clear(); strumien.close(); return true; }
bool Labirynt::Narysuj() { switch( TRYB ) { case M_EXIT: if( NazwaPliku() && OtworzPlik() ) TRYB = M_GAME; else return false; break; case M_GAME: system( "cls" ); cout << string( 40, '*' ) << "\n*" << center( "Gra LABIRYNT", 38 ) << "*\n" << string( 40, '*' ) << endl; cout << "\n\n\n\t" << string( 22, '*' ) << endl; for( int y = 0; y < 20; y++ ) { cout << "\t*"; for( int x = 0; x < 20; x++ ) cout << static_cast < char >( sPlansza.PLANSZA[ y ][ x ] ); cout << "*\n"; } cout << "\t" << string( 22, '*' ) << "\n\n\n" << endl; cout << "\t\tLEGENDA\n" << "'$' GRACZ" << "\t'@' META\n" << "'|'\tSCIANY\t'-'\n" << "PORUSZASZ SIE STRZALKAMI\n" << "Wykonales " << iLicznik << " ruchow."; break; case M_WIN: system( "cls" ); cout << string( 40, '*' ) << "\n*" << center( "Gra LABIRYNT", 38 ) << "*\n" << string( 40, '*' ) << endl; cout << "\n\n\n\t" << string( 22, '*' ) << endl; for( int y = 0; y < 20; y++ ) { cout << "\t*"; for( int x = 0; x < 20; x++ ) cout << static_cast < char >( sPlansza.PLANSZA[ y ][ x ] ); cout << "*\n"; } cout << "\t" << string( 22, '*' ) << "\n\n\n" << endl; cout << "Przeszles labirynt w " << iLicznik << " ruchow."; break; case M_EDIT: system( "cls" ); cout << string( 40, '*' ) << "\n*" << center( "EDYTOR", 38 ) << "*\n" << string( 40, '*' ) << endl; cout << "\n\n\n\t" << string( 22, '*' ) << endl; for( int y = 0; y < 20; y++ ) { cout << "\t*"; for( int x = 0; x < 20; x++ ) cout << static_cast < char >( sDodatkowa.PLANSZA[ y ][ x ] ); cout << "*\n"; } cout << "\t" << string( 22, '*' ) << "\n\n\n" << endl; cout << "\t\tLEGENDA\n" << "1 - SCIANA '|'\t2 - SCIANA '-'\n" << "3 - GRACZ '$'\t4 - META '@'\n" << "5 - TLO ' '\n" << "\tESC - WYJSCIE BEZ ZAPISU ZMIAN\tENTER - ZATWIERDZ ZMIANY\n" << "PORUSZASZ SIE WSKAZNIKIEM '#'\n" << "ZA POMOCA STRZALEK"; break; } return true; }
bool Labirynt::Ruch() { int x1, y1; int znak = getch(); switch( TRYB ) { case M_GAME: switch( znak ) { case 224: if( kbhit() ) { znak = getch(); switch( znak ) { case 72: x1 = sGracz.x; y1 = sGracz.y - 1; if( sPlansza.PLANSZA[ y1 ][ x1 ] == TLO ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.y = y1; iLicznik++; } else if( sPlansza.PLANSZA[ y1 ][ x1 ] == META ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.y = y1; iLicznik++; TRYB = M_WIN; } else iLicznik++; break; case 80: x1 = sGracz.x; y1 = sGracz.y + 1; if( sPlansza.PLANSZA[ y1 ][ x1 ] == TLO ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.y = y1; iLicznik++; } else if( sPlansza.PLANSZA[ y1 ][ x1 ] == META ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.y = y1; iLicznik++; TRYB = M_WIN; } else iLicznik++; break; case 75: x1 = sGracz.x - 1; y1 = sGracz.y; if( sPlansza.PLANSZA[ y1 ][ x1 ] == TLO ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.x = x1; iLicznik++; } else if( sPlansza.PLANSZA[ y1 ][ x1 ] == META ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.x = x1; iLicznik++; TRYB = M_WIN; } else iLicznik++; break; case 77: x1 = sGracz.x + 1; y1 = sGracz.y; if( sPlansza.PLANSZA[ y1 ][ x1 ] == TLO ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.x = x1; iLicznik++; } else if( sPlansza.PLANSZA[ y1 ][ x1 ] == META ) { sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sPlansza.PLANSZA[ y1 ][ x1 ] = GRACZ; sGracz.x = x1; iLicznik++; TRYB = M_WIN; } else iLicznik++; break; } } break; case 76: sPlansza.PLANSZA[ sMeta.y ][ sMeta.x ] = GRACZ; sPlansza.PLANSZA[ sGracz.y ][ sGracz.x ] = TLO; sGracz.x = sMeta.x; sGracz.y = sMeta.y; iLicznik++; TRYB = M_WIN; break; } break; case M_EDIT: switch( znak ) { case 224: if( kbhit() ) { znak = getch(); switch( znak ) { case 72: x1 = sWskaznik.x; y1 = sWskaznik.y - 1; if( y1 < 0 ) break; for( int y = 0; y < 20; y++ ) for( int x = 0; x < 20; x++ ) sDodatkowa.PLANSZA[ y ][ x ] = sPlansza.PLANSZA[ y ][ x ]; sWskaznik.x = x1; sWskaznik.y = y1; sDodatkowa.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = EDIT; break; case 80: x1 = sWskaznik.x; y1 = sWskaznik.y + 1; if( y1 > 19 ) break; for( int y = 0; y < 20; y++ ) for( int x = 0; x < 20; x++ ) sDodatkowa.PLANSZA[ y ][ x ] = sPlansza.PLANSZA[ y ][ x ]; sWskaznik.x = x1; sWskaznik.y = y1; sDodatkowa.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = EDIT; break; case 75: x1 = sWskaznik.x - 1; y1 = sWskaznik.y; if( x1 < 0 ) break; for( int y = 0; y < 20; y++ ) for( int x = 0; x < 20; x++ ) sDodatkowa.PLANSZA[ y ][ x ] = sPlansza.PLANSZA[ y ][ x ]; sWskaznik.x = x1; sWskaznik.y = y1; sDodatkowa.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = EDIT; break; case 77: x1 = sWskaznik.x + 1; y1 = sWskaznik.y; if( x1 > 19 ) break; for( int y = 0; y < 20; y++ ) for( int x = 0; x < 20; x++ ) sDodatkowa.PLANSZA[ y ][ x ] = sPlansza.PLANSZA[ y ][ x ]; sWskaznik.x = x1; sWskaznik.y = y1; sDodatkowa.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = EDIT; break; } } break; case 49: sPlansza.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = SCIANA1; break; case 50: sPlansza.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = SCIANA2; break; case 51: sPlansza.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = GRACZ; break; case 52: sPlansza.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = META; break; case 53: sPlansza.PLANSZA[ sWskaznik.y ][ sWskaznik.x ] = TLO; break; case 27: return false; break; case 13: TRYB = M_EXIT; break; } break; } return true; }
bool Labirynt::NEW() { TRYB = M_EDIT; system( "cls" ); for( int y = 0; y < 20; y++ ) for( int x = 0; x < 20; x++ ) sDodatkowa.PLANSZA[ y ][ x ] = TLO; while( true ) { while( TRYB == M_EDIT ) { Narysuj(); if( !Ruch() ) { cout << "\n\nWYCHODZE BEZ ZAPISYWANIA ZMIAN"; getch(); return false; } } if( CHECKS() ) break; else { cout << "\nPLANSZY NIE DA SIE UKONCZYC"; getch(); TRYB = M_EDIT; } } system( "cls" ); cout << "\n\n\n\tPODAJ NAZWE MAPY: "; cin >> sPlansza.cNazwaPlanszy; string napis = "MAPS\\"; napis += sPlansza.cNazwaPlanszy; napis += ".adi"; fstream zapis( napis.c_str(), ios::out | ios::binary ); if( zapis ) { zapis.write( reinterpret_cast < char *>( & sPlansza ), sizeof( sPlansza ) ); cout << "\nPLANSZA ZAPISANA"; getch(); } return true; }
bool Labirynt::EDITION() { TRYB = M_EDIT; system( "cls" ); int i_nrPlanszy; vector < string > VECs_Nazwa; if( Wypisz_pliki( VECs_Nazwa, i_nrPlanszy ) ) { string wybor = "MAPS\\" + VECs_Nazwa[ i_nrPlanszy ]; fstream plik( wybor.c_str(), ios::in | ios::out | ios::binary ); if( plik ) { plik.read( reinterpret_cast < char *>( & sPlansza ), sizeof( sPlansza ) ); while( true ) { while( TRYB == M_EDIT ) { Narysuj(); if( !Ruch() ) { cout << "\n\nWYCHODZE BEZ ZAPISYWANIA ZMIAN"; getch(); return false; } } if( CHECKS() ) break; else { cout << "\nPLANSZY NIE DA SIE UKONCZYC"; getch(); TRYB = M_EDIT; } } plik.seekp( 0, ios::beg ); plik.write( reinterpret_cast < char *>( & sPlansza ), sizeof( sPlansza ) ); } } return true; }
bool Labirynt::CHECKS() { for( int y = 0; y < 20; y++ ) for( int x = 0; x < 20; x++ ) sDodatkowa.PLANSZA[ y ][ x ] = sPlansza.PLANSZA[ y ][ x ]; for(;; ) { if( sDodatkowa.PLANSZA[ sGracz.y - 1 ][ sGracz.x ] == META || sDodatkowa.PLANSZA[ sGracz.y + 1 ][ sGracz.x ] == META || sDodatkowa.PLANSZA[ sGracz.y ][ sGracz.x - 1 ] == META || sDodatkowa.PLANSZA[ sGracz.y ][ sGracz.x + 1 ] == META ) return true; else if( sDodatkowa.PLANSZA[ sGracz.y - 1 ][ sGracz.x ] == TLO ) { sDodatkowa.PLANSZA[ sGracz.y - 1 ][ sGracz.x ] = CHECK; sGracz.y = sGracz.y - 1; } else if( sDodatkowa.PLANSZA[ sGracz.y + 1 ][ sGracz.x ] == TLO ) { sDodatkowa.PLANSZA[ sGracz.y + 1 ][ sGracz.x ] = CHECK; sGracz.y = sGracz.y + 1; } else if( sDodatkowa.PLANSZA[ sGracz.y ][ sGracz.x - 1 ] == TLO ) { sDodatkowa.PLANSZA[ sGracz.y ][ sGracz.x - 1 ] = CHECK; sGracz.x = sGracz.x - 1; } else if( sDodatkowa.PLANSZA[ sGracz.y ][ sGracz.x - 1 ] == TLO ) { sDodatkowa.PLANSZA[ sGracz.y ][ sGracz.x + 1 ] = CHECK; sGracz.x = sGracz.x + 1; } else { int y = 0, x; bool wyjscie = true; while( y < 20 && wyjscie == true ) { x = 0; while( x < 20 ) { if( sDodatkowa.PLANSZA[ y ][ x ] == CHECK ) if( sDodatkowa.PLANSZA[ y - 1 ][ x ] == TLO || sDodatkowa.PLANSZA[ y + 1 ][ x ] == TLO || sDodatkowa.PLANSZA[ y ][ x - 1 ] == TLO || sDodatkowa.PLANSZA[ y ][ x + 1 ] == TLO ) { sGracz.y = y; sGracz.x = x; wyjscie = false; break; } x++; } y++; } if( wyjscie == true ) return false; } } }
bool Wypisz_pliki( vector < string > & SPIS, int & wyb ) { DIR * pdir = NULL; struct dirent * ps_dirent = NULL; pdir = opendir( ".MAPS" ); if( pdir ) { while( ps_dirent = readdir( pdir ) ) { if( !( strstr( ps_dirent->d_name, "adi" ) == NULL ) ) SPIS.push_back( ps_dirent->d_name ); } if( SPIS.empty() ) { cout << "\n\n\n\tW PLIKU \"MAPS\" NIE MA MAP"; getch(); return false; } while( true ) { system( "cls" ); cout << "\n\n\tLISTA MAP:\n"; for( int i = 0; i <( static_cast < int >( SPIS.size() ) ); i++ ) cout << "\n\t" << i + 1 << " " << SPIS[ i ]; cout << "\nNUMER: "; if( zab_cin( wyb ) && wyb >= 0 && wyb <( static_cast < int >( SPIS.size() ) ) ) break; cout << "\nNIE MA TAKIEJ OPCJI."; getch(); } } else { cout << "\nNIE UDALO SIE OTWOZYC SCIEZKI."; getch(); return false; } closedir( pdir ); return true; }
void DELETE_MAP() { system( "cls" ); vector < string > VECs_Nazwy; int i_liczba; if( Wypisz_pliki( VECs_Nazwy, i_liczba ) ) { string s_Wybrana = "MAPS\\" + VECs_Nazwy[ i_liczba ]; remove( s_Wybrana.c_str() ); } }
|
|
Monika90 |
» 2014-02-20 22:38:41 Definiując funkcje Wypisz_pliki i DELETE_MAP zapomniałeś napisać Labirynt:: przed ich nazwami.
|
|
Haker123 Temat założony przez niniejszego użytkownika |
» 2014-02-21 17:12:34 Kurcze , taki podstawowy błąd i ja go nie wyłapałem przez 5h ;/ Dzieki wielkie ;]
Wiem że nie powinno się w jednym temacie zadawać innych pytań ale te dotyczą tego samego kodu.
1.W funkcji CHECKS zastosowałem dużo zagnieżdżonych if else, tak można robić czy powinienem zrobić to inaczej?? 2.Czy powinienem w którymś miejscu dodać jakieś zabezpieczenia przy kożystaniu z plików, albo jakieś są nie potrzebne i powinienem je wyrzucić??(chodzi mi o sytuacje jak np plik się nie otworzył , nie można zapisać do pliku) |
|
DejaVu |
» 2014-02-21 18:07:03 Ad 1. Można.
Ad 2. Generalnie nie zajmujemy się analizowaniem czyjegoś kodu. Jeżeli kod działa poprawnie to nie masz nad czym dumać. Kod źródłowy nigdy nie osiąga stanu idealnego. |
|
Haker123 Temat założony przez niniejszego użytkownika |
» 2014-02-22 17:01:47 Dzieki za pomoc ;] Temat można zamknąć. |
|
« 1 » |