[C++] Szablony, a błędy linkera - undefined reference to...
Ostatnio zmodyfikowano 2012-09-23 17:20
Admixior Temat założony przez niniejszego użytkownika |
[C++] Szablony, a błędy linkera - undefined reference to... » 2012-09-22 23:55:42 Output: ------ Rebuild All started: Project: Snake, Configuration: Debug AVR ------ C:\Program Files (x86)\Atmel\Atmel Studio 6.0\make\make.exe clean all Snake.o: In function `Snake::ResetGame()': C:\Users\Admixior\documents\atmel studio\Snake\Snake\Debug/.././Snake.cpp(11,1): undefined reference to `Queue<Snake::point>::EraseElement()' C:\Users\Admixior\documents\atmel studio\Snake\Snake\Debug/.././Snake.cpp(11,1): undefined reference to `Queue<Snake::point>::GetFirstElement()' C:\Users\Admixior\documents\atmel studio\Snake\Snake\Debug/.././Snake.cpp(15,1): undefined reference to `Queue<Snake::point>::AddElement(Snake::point&)' C:\Users\Admixior\documents\atmel studio\Snake\Snake\Debug/.././Snake.cpp(17,1): undefined reference to `Queue<Snake::point>::AddElement(Snake::point&)' C:\Users\Admixior\documents\atmel studio\Snake\Snake\Debug/.././Snake.cpp(19,1): undefined reference to `Queue<Snake::point>::AddElement(Snake::point&)' C:\Users\Admixior\documents\atmel studio\Snake\Snake\Debug/.././Snake.cpp(21,1): undefined reference to `Queue<Snake::point>::AddElement(Snake::point&)' collect2: ld returned 1 exit status make: *** [Snake.elf] Error 1 Done building project "Snake.cppproj" -- FAILED.
Build FAILED. ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Input w skrócie: Snake.cpp void Snake::ResetGame() { while( snake_point.GetFirstElement() ) snake_point.EraseElement(); point p; p.x = 4; p.y = 7; snake_point.AddElement( p ); p.y = 6; snake_point.AddElement( p ); p.y = 5; snake_point.AddElement( p ); p.y = 4; snake_point.AddElement( last_point = p ); crashed = false; }
Snake.h class Snake { public: private: struct point { BYTE x, y; }; Queue < point > snake_point; };
Queue.h template < class typ > class Queue { struct wezel { typ * poprzedni, * nastepny; typ aktualny; }; wezel * start; wezel * act; wezel * koniec; public: Queue() : start( 0 ) , koniec( 0 ) , act( 0 ) { } void AddElement( typ & add ); void EraseElement(); typ * GetFirstElement(); typ * GetNextElement(); };
Queue.cpp #include "Queue.h"
template < class typ > void Queue < typ >::AddElement( typ & add ) { } template < class typ > void Queue < typ >::EraseElement() { } template < class typ > typ * Queue < typ >::GetFirstElement() { } template < class typ > typ * Queue < typ >::GetNextElement() { }
PYTANIE: Dlaczego??? Jak przecież jest definicja! |
|
DejaVu |
» 2012-09-23 04:56:43 Szablony nie są kompilowalne, a więc ich implementacja musi być umieszczona w pliku nagłówkowym. W przeciwnym wypadku implementacja nie będzie widoczna i stąd się biorą Twoje błędy kompilacji. |
|
Admixior Temat założony przez niniejszego użytkownika |
» 2012-09-23 11:57:19 Tudzież wszystkie funkcje queue.cpp muszę zinkludować do queue.h? Czy jest jakiś lepszy sposób niż kopiowanie wszystkich funkcji do nagłówka? Tak żeby był podział na 2 pliki?
EDIT: OK mam wystarczy w pliku który używał kolejki zamiast queue.h napisać queue.cpp i wtedy miałem oba pliki ;]
Za to dalej są undefined reference: undefined reference to `operator delete(void*)' undefined reference to `operator new(unsigned int)'
PS. Dodam że kod jest kompilowany pod Atmel Studio na procek AVR, a tam ... nie ma chyba tych libek |
|
Mrovqa |
» 2012-09-23 13:21:34 Nie pisałem nigdy kodu pod mikrokontrolery (w wakacje miałem chęci, gorzej z nauką elektroniki i kupna części :P), ale skoro nie widzi operatorów new i delete to spróbuj użyć malloc i free. Może zadziała ;) |
|
Admixior Temat założony przez niniejszego użytkownika |
» 2012-09-23 17:20:03 Ok znalazłem na necie przykład definicji i działa ;] |
|
« 1 » |