| | |
undefined reference error -HELP!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
hello all.
This program is just a collection of functions to demonstrate various programming principles. What I'm
saying is that don't worry about the overall function-it ain't all that...
One of the principles is using a 'non member' friend function to display the output.
I added this to the public functions and it's definition appears just below the class declaration.
I get warning about it's status (OK), but then the fatal error is
the source:
I'm sure you eagle -eyed pro's can spot the problem in a New York minute, but I've been straring at this too long!
Thanks in advance!
This program is just a collection of functions to demonstrate various programming principles. What I'm
saying is that don't worry about the overall function-it ain't all that...
One of the principles is using a 'non member' friend function to display the output.
I added this to the public functions and it's definition appears just below the class declaration.
I get warning about it's status (OK), but then the fatal error is
C++ Syntax (Toggle Plain Text)
templ2.cpp: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Array<int>&)'
the source:
C++ Syntax (Toggle Plain Text)
// Implementation of the Template Array #include <iostream> using namespace std; const int DefaultSize =10; //declare a simple Animal class so that we can create an //array of animals class Animal { public: Animal(int) ; Animal(); ~Animal() {} int GetWeight () const { return itsWeight;} void Display() const {std::cout << itsWeight;} private: int itsWeight; }; Animal::Animal(int weight): itsWeight(weight) { } Animal::Animal():itsWeight(0) {} template <class T> //declare the template and the parameter class Array // the class being paramterized { public: //costrucyors Array(int itsSize = DefaultSize); Array(const Array &rhs); ~Array() {delete [] ptype;} //operators Array& operator = (const Array&); T& operator [] (int offset) {return ptype[offset];} const T& operator[] (int offset) const {return ptype[offset];} //accessors int GetSize() const {return itsSize;} //template <class T>//compiler needs this if non member friend friend ostream& operator << (ostream&, Array<T>&); private : T *ptype; int itsSize; }; template <class T> ostream& operator << (ostream& output, Array<T>& theArray) { for (int i = 0; i < theArray.itsSize; i++) output << "[" << i << "]" << theArray[i] <<endl; return output; } //implementations follow... //implement the constructor template <class T> Array<T>::Array(int size): itsSize(size) { ptype = new T[size]; //the constructors of the type created //should be of default value } //copy constructor 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]; } //operator= template <class T> Array<T>& Array<T>::operator=(const Array &rhs) { if (this == &rhs) return *this; delete [] ptype; itsSize = rhs.GetSize(); ptype = new T[itsSize]; for (int i =0; i<itsSize; i++) ptype[i] =rhs[i]; return *this; } //driver program int main() { bool Stop = false; //looping flag int offset , value; Array<int> theArray; while (Stop = false) { cout << "Enter an offset (0-9)"; cout << "and a value. (-1 to stop): "; cin >> offset >>value; if (offset < 0 ) break; if (offset > 9) { cout << "*** Please use values between 0 and 9. ***\n"; continue; } theArray[offset] = value; } cout << "\nHere's the entire array:\n"; cout << theArray << endl; return 0; }
I'm sure you eagle -eyed pro's can spot the problem in a New York minute, but I've been straring at this too long!
Thanks in advance!
•
•
•
•
I'm sure you eagle -eyed pro's can spot the problem in a New York minute, but I've been straring at this too long!
What you are trying to do here is to create a function which will befriend all the specializations of the Array class, which I don't think any compiler conforming with the standards will allow you to do.
There are some things which you have to do to get around this...
1. Forward declare the class, along with the templated overloaded function.
C++ Syntax (Toggle Plain Text)
template <class T> class Array ; template <class T> ostream& operator<< ( ostream&, Array<T>& ) ;
C++ Syntax (Toggle Plain Text)
template <class T> //declare the template and the parameter class Array // the class being paramterized { public: .... friend ostream& operator<< <>(ostream&, Array<T>&); private : .... };
For an interesting read look here.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Yep! That's the way it works. Seems like alot of trouble to make the direct acess friend function to compile.
here's the final draft that compiles using the friend function.
Note that the template id operator <> is only used where is declared as a public function. The definition doesn't need it. That rather surprises me...
Thanks for your input!
here's the final draft that compiles using the friend function.
Note that the template id operator <> is only used where is declared as a public function. The definition doesn't need it. That rather surprises me...
C++ Syntax (Toggle Plain Text)
template <class T > class Array; template <class T> ostream& operator << (ostream& , Array<T>& ); template <class T> //declare the template and the parameter class Array // the class being paramterized { public: //costrucyors Array(int itsSize = DefaultSize); Array(const Array &rhs); ~Array() {delete [] ptype;} //operators Array& operator = (const Array&); T& operator [] (int offset) {return ptype[offset];} const T& operator[] (int offset) const {return ptype[offset];} //accessors int GetSize() const {return itsSize;} friend ostream& operator << <> (ostream&, Array<T>&); private : T *ptype; int itsSize; }; template <class T > ostream& operator << (ostream& output, Array<T>& theArray) { for (int i = 0; i < theArray.itsSize; i++) output << "[" << i << "]" << theArray[i] <<endl; return output; }
![]() |
Similar Threads
- compiling using g++ results to undefined reference (C++)
- Dev C++ linker errors, undefined reference (C++)
- undefined reference to 'main' (C++)
- [Linker error] undefined reference to `CQTMovieFile::CQTMovieFile()' (C++)
- What is it meaned [Linker error] undefined reference to `MAPILogon@24' (C++)
- Dev-C++, GLUI linker error, undefined reference (C++)
- Linker Error>Undefined reference error to xyz (C++)
- undefined reference errors when using C++ Sockets Library (C++)
Other Threads in the C++ Forum
- Previous Thread: Dev C++ linker errors, undefined reference
- Next Thread: [SOS] How to built a interface program whit code::blocks?
Views: 3753 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






