From: "A. Jans-Beken" Newsgroups: comp.os.msdos.djgpp Subject: C++ array problem Date: Sun, 13 Dec 1998 22:41:11 +0100 Organization: World Access Lines: 170 Message-ID: <36743477.F562AF4F@wxs.nl> NNTP-Posting-Host: vl0339-0.dial.wxs.nl Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: reader1.wxs.nl 913584969 3315 195.121.19.83 (13 Dec 1998 21:36:09 GMT) X-Complaints-To: abuse AT wxs DOT nl NNTP-Posting-Date: 13 Dec 1998 21:36:09 GMT X-Mailer: Mozilla 4.04 [en] (Win95; I) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com I was experimenting with a template class for arrays. This is an example in the C++ primer from stanly B. Lippman 2nd edition 1991. During compiling (or actually I believe its linking) I get this error message (in a rhide status screen): --- Creating: array.exe Error: arrmain.o: In function `main': arrmain.cpp(4) Error: undefined reference to `ARRAY::init(int const *, int)' arrmain.cpp(7) Error: undefined reference to `__ls__H1Zi_R7ostreamRt5ARRAY1ZX01_R7ostream' There were some errors --- The init function IS defined. I can't figure out why its complaining about this. The second error, well I can't cook soup from it. Someone knows what is happening here? Are there some examples of variable sized one and two dimensional arrays out there? Please respond (also) via e-mail... Complete code follows: ---vvv---array.hpp // // array.hpp // ========= // #ifndef ARRAY_HPP #define ARRAY_HPP template class ARRAY; template ostream& operator<<(ostream&, ARRAY&); const int DEF_ARRAY_SIZE = 10; template class ARRAY { private: int size; Type *ia; // void swap(const int, const int); void init(const Type *, int); public: // Constructors ... ARRAY(int asize = DEF_ARRAY_SIZE) { init(NULL, asize); } ARRAY(const Type *ar, int asize) { init(ar,asize); } ARRAY(const ARRAY& src_array) { init(src_array.ia, src_array.size); } // Destructor ... ~ARRAY() { delete [] ia; } // Operators #ifndef NDEBUG // friend ostream& operator<<(ostream&, ARRAY&); #endif ARRAY& operator=(const ARRAY&); Type& operator[](const int ix) { return ia[ix]; } // Members & Methods #ifndef NDEBUG void print(ostream&); #endif int get_size() { return size; } void grow(); }; #endif ---vvv---array.cpp // // array.cpp // ========= // #ifndef NDEBUG #include #endif #include #include "array.hpp" // Operators ... #ifndef NDEBUG template ostream& operator<<(ostream& os, ARRAY& a) { a.print(os); return os; } #endif template ARRAY& ARRAY::operator=(const ARRAY& src_array) { if (this == &src_array) return *this; delete [] ia; init(src_array.ia, src_array.size); return *this; } // Members ... #ifndef NDEBUG template void ARRAY::print(ostream& os) { // Address of array... // os << "($" << hex << &t << dec << ") "; os << "Array $" << size << "\n"; // << hex << *a << dec << ": "; // Values of array-values... // for (int i = 0; i < a.size; i++) { // os << a.ia[i] << " "; } #endif template void ARRAY::init(const Type *src_array, int n) { ia = new Type[size = n]; assert(ia != NULL); for (int i = 0; i < size; i++) ia[i] = (src_array != NULL) ? src_array[i] : (Type) 0; } template void ARRAY::grow() { Type *old_array = ia; int old_size = size; int new_size = old_size + (old_size/2) + 1; ia = new Type[size = new_size]; assert(ia != NULL); // for (int i = 0; i < old_size; i++) // ia[i] = old_array[i]; // for ( ; i < size; i++) // ia[i] = (Type) 0; for (int i = 0; i < size; i++) ia[i] = (i < old_size) ? old_array[i] : (Type) 0; } ---vvv---arrmain.cpp /* * arrmain.cpp */ #include #include #include #include "array.hpp" int main () { MSS_LOG_MSG("Starting a LOG session"); cout << "Testing the array class\n"; cout << "-----------------------\n"; ARRAY arint; cout << arint << "\n"; getch(); return 0; } ---