Hi all...
Second thread start in 24 hours... :-S
I'm having a nightmare trying to get the below code to link, I keep getting unresolved externals errors from the linker. It seems to be related to use of the overloaded operator << in the "main" function, which is being used to print each object in an array created from a template.
Apologies for the looooong code... thought best to just print the lot rather than being asked for it later!...
I've been over the code with a fine tooth comb - I can't find any typos or something that would cause functions declarations not to match with their definitions (although I've just copy/pasted this from Visual Express so don't be surprised if I've accidentally deleted a semi colon or something). Please help me before I throw my rather nice laptop out the lounge window....Code:#include <iostream> using namespace std; const int defaultSize = 3; class Dog { public: Dog (); Dog (int); ~Dog (); int getAge () const {return itsAge;} void setAge (int age) {itsAge = age;} friend ostream& operator << (ostream&, const Dog&); private: int itsAge; }; Dog::Dog () : itsAge(0) { cout << "Dog default constructor..."; } Dog::Dog (int age) : itsAge (age) { cout << "Dog constructor with parameter..."; } Dog::~Dog() { cout << "Destructed a dog :-(..."; } ostream& operator << (ostream& output, const Dog& rhs) { output << rhs.getAge(); // equates to cout << itsAge; return output; } template <class T> class Array { public: Array (int itsSize = defaultSize); Array (const Array &rhs); ~Array () {delete [] pType;} Array& operator= (const Array &); T& operator [] (int offset) {return pType[offset];} const T& operator [] (int offset) const {return pType[offset];} int getSize () const {return itsSize;} friend ostream& operator << (ostream&, const Array<T>&); private: T * pType; int itsSize; }; template <class T> Array<T>::Array(int size = defaultSize) : itsSize(size) { pType = new T [size]; for (int i = 0; i < size; i++) pType[i] = (T)0; } template <class T> Array<T>::Array(const Array &rhs) { itsSize = rhs.getSize(); pType = new T[itsSize]; for (int i = 0; i < itsSize; i++) pType[i] = rhs[i]; // uses const operator [] } template <class T> Array<T>& Array<T>::operator = (const Array &rhs) { if (this == &rhs) return *this; delete [] pType; pType = new T[itsSize]; for (int i = 0; i < itsSize; i++) pType[i] = rhs[i] } Array<Dog>::Array(int AnimalArraySize) : itsSize(AnimalArraySize) { pType = new Dog[AnimalArraySize]; } template <class T> ostream& operator << (ostream& output, const Array<T>& theArray) { for (int i=0; i < theArray.getSize(); i++) output << "[" << i << "]: " << theArray[i] << endl; return output; } void intFillFunction (Array<int>& intArray); void dogFillFunction (Array<Dog>& theKennel); int main () { Array<int> intArray; Array<Dog> theKennel; intFillFunction (intArray); dogFillFunction (theKennel); cout << intArray << endl; // CAUSING UNRESOLVED EXTERNAL PROBLEM cout << theKennel << endl; // CAUSING UNRESOLVED EXTERNAL PROBLEM return 0; } void intFillFunction (Array<int>& theArray) { bool Stop = false; int offset, value; while (!Stop) { cout << "Enter an offset (0-2) and a value. (-1 to stop)"; cin >> offset >> value; if (offset < 0) break; if (offset > 2) { cout << "Value between 0-2 only please.\n"; continue; } theArray[offset] = value; } } void dogFillFunction (Array<Dog>& dogArray) { Dog * pDog; for (int i = 0; i < dogArray.getSize(); i++) { pDog = new Dog (i*3); dogArray[i] = *pDog; delete pDog; pDog = 0; } }
Also can anyone give me any tips for tracking down linker errors? Visual Express can't track them down in the same way it can with complier errors... cheers :-)