From: caramaith Newsgroups: comp.os.msdos.djgpp Subject: Re: odd compiler error in DJGPP... Date: Sun, 05 Mar 2000 12:04:08 +0100 Organization: T-Online Lines: 171 Message-ID: <38C23F28.FD812219@myokay.net> References: <38C17CB3 DOT BCC06203 AT myokay DOT net> <38C1A7B4 DOT AF973AB7 AT myokay DOT net> <1rs6csgsc9mcr6f4sp406tapu7jlc7n7ho AT 4ax DOT com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news00.btx.dtag.de 952254379 19811 320054133135-0001 000305 11:06:19 X-Complaints-To: abuse AT t-online DOT de X-Sender: 320054133135-0001 AT t-dialin DOT net X-Mailer: Mozilla 4.7 [de] (Win98; I) X-Accept-Language: de To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Jason Green schrieb: > > caramaith wrote: > > > help, I can't see what I did wrong.... > > Neither can we! ;-) > > Show us the full source code. > Show us the exact compile line(s). > > Also please say which example number this is from the TIC++ book. it's the CppLib.h, CppLib.cpp and CppLibTest.cpp from Chapter 4 of Volume One of TIC++. Here they come... ----------------- //: C04:CppLib.h // C-like library converted to C++ struct Stash { int size; // Size of each space int quantity; // Number of storage spaces int next; // Next empty space // Dynamically allocated array of bytes: unsigned char* storage; // Functions! void initialize(int size); void cleanup(); int add(const void* element); void* fetch(int index); int count(); void inflate(int increase); }; ///:~ ----------------- //: C04:CppLib.cpp {O} // C library converted to C++ // Declare structure and functions: #include "CppLib.h" #include #include using namespace std; // Quantity of elements to add // when increasing storage: const int increment = 100; void Stash::initialize(int sz) { size = sz; quantity = 0; storage = 0; next = 0; } int Stash::add(const void* element) { if(next >= quantity) // Enough space left? inflate(increment); // Copy element into storage, // starting at next empty space: int startBytes = next * size; unsigned char* e = (unsigned char*)element; for(int i = 0; i < size; i++) storage[startBytes + i] = e[i]; next++; return(next - 1); // Index number } void* Stash::fetch(int index) { // Check index boundaries: assert(0 <= index); if(index >= next) return 0; // To indicate the end // Produce pointer to desired element: return &(storage[index * size]); } int Stash::count() { return next; // Number of elements in CStash } void Stash::inflate(int increase) { assert(increase > 0); int newQuantity = quantity + increase; int newBytes = newQuantity * size; int oldBytes = quantity * size; unsigned char* b = new unsigned char[newBytes]; for(int i = 0; i < oldBytes; i++) b[i] = storage[i]; // Copy old to new delete []storage; // Old storage storage = b; // Point to new memory quantity = newQuantity; } void Stash::cleanup() { if(storage != 0) { cout << "freeing storage" << endl; delete []storage; } } ///:~ ----------------- //: C04:CppLibTest.cpp //{L} CppLib // Test of C++ library #include "CppLib.h" #include "../require.h" #include #include #include using namespace std; int main() { Stash intStash; intStash.initialize(sizeof(int)); for(int i = 0; i < 100; i++) intStash.add(&i); for(int j = 0; j < intStash.count(); j++) cout << "intStash.fetch(" << j << ") = " << *(int*)intStash.fetch(j) << endl; // Holds 80-character strings: Stash stringStash; const int bufsize = 80; stringStash.initialize(sizeof(char) * bufsize); ifstream in("CppLibTest.cpp"); assure(in, "CppLibTest.cpp"); string line; while(getline(in, line)) stringStash.add(line.c_str()); int k = 0; char* cp; while((cp =(char*)stringStash.fetch(k++)) != 0) cout << "stringStash.fetch(" << k << ") = " << cp << endl; intStash.cleanup(); stringStash.cleanup(); } ///:~ ----------------- ...a very easy piece of code in my opinion. the exact compiler error goes like this: CppLibTest.cpp(2) Error: undefined reference to 'Stash::initialize(int)' CppLibTest.cpp(4) Error: undefined reference to 'Stash::add(void const*)' CppLibTest.cpp(5) Error: undefined reference to 'Stash::count(void)' CppLibTest.cpp(8) Error: undefined reference to 'Stash::fetch(int)' CppLibTest.cpp(12) Error: undefined reference to 'Stash::initialize(int)' CppLibTest.cpp(17) Error: undefined reference to 'Stash::add(void const*)' CppLibTest.cpp(20) Error: undefined reference to 'Stash::fetch(int)' CppLibTest.cpp(23) Error: undefined reference to 'Stash::cleanup(void)' CppLibTest.cpp(24) Error: undefined reference to 'Stash::cleanup(void)' Error: collect2: ld returned 1 exit status ....just referring to all functions called in the CppLibTester.cpp. Maybe there's something special I have to be aware of when trying to link c++, but I don't try to link any standard templates or whatsoever.... greez cara