undefined reference error -HELP!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 232
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

undefined reference error -HELP!

 
0
  #1
Dec 18th, 2006
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
  1. templ2.cpp: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Array<int>&)'
  2.  

the source:
  1. // Implementation of the Template Array
  2. #include <iostream>
  3. using namespace std;
  4. const int DefaultSize =10;
  5.  
  6. //declare a simple Animal class so that we can create an
  7. //array of animals
  8.  
  9. class Animal
  10. {
  11. public:
  12. Animal(int) ;
  13. Animal();
  14. ~Animal() {}
  15. int GetWeight () const { return itsWeight;}
  16. void Display() const {std::cout << itsWeight;}
  17. private:
  18. int itsWeight;
  19. };
  20.  
  21. Animal::Animal(int weight):
  22. itsWeight(weight)
  23. { }
  24.  
  25. Animal::Animal():itsWeight(0)
  26. {}
  27.  
  28.  
  29. template <class T> //declare the template and the parameter
  30. class Array // the class being paramterized
  31. {
  32. public:
  33. //costrucyors
  34. Array(int itsSize = DefaultSize);
  35. Array(const Array &rhs);
  36. ~Array() {delete [] ptype;}
  37.  
  38. //operators
  39. Array& operator = (const Array&);
  40. T& operator [] (int offset) {return ptype[offset];}
  41. const T& operator[] (int offset) const
  42. {return ptype[offset];}
  43. //accessors
  44. int GetSize() const {return itsSize;}
  45. //template <class T>//compiler needs this if non member friend
  46. friend ostream& operator << (ostream&, Array<T>&);
  47.  
  48.  
  49.  
  50. private :
  51. T *ptype;
  52. int itsSize;
  53. };
  54.  
  55. template <class T>
  56. ostream& operator << (ostream& output, Array<T>& theArray)
  57. {
  58. for (int i = 0; i < theArray.itsSize; i++)
  59. output << "[" << i << "]" << theArray[i] <<endl;
  60.  
  61. return output;
  62. }
  63.  
  64. //implementations follow...
  65.  
  66. //implement the constructor
  67. template <class T>
  68. Array<T>::Array(int size):
  69. itsSize(size)
  70. {
  71. ptype = new T[size];
  72. //the constructors of the type created
  73. //should be of default value
  74. }
  75.  
  76. //copy constructor
  77. template <class T>
  78. Array<T>::Array(const Array &rhs)
  79. {
  80. itsSize =rhs.GetSize();
  81. ptype = new T[itsSize];
  82. for (int i=0; i<itsSize; i++)
  83. ptype[i] = rhs[i];
  84. }
  85. //operator=
  86.  
  87. template <class T>
  88. Array<T>& Array<T>::operator=(const Array &rhs)
  89. {
  90. if (this == &rhs)
  91. return *this;
  92. delete [] ptype;
  93. itsSize = rhs.GetSize();
  94. ptype = new T[itsSize];
  95. for (int i =0; i<itsSize; i++)
  96. ptype[i] =rhs[i];
  97. return *this;
  98. }
  99.  
  100. //driver program
  101.  
  102. int main()
  103.  
  104. {
  105. bool Stop = false; //looping flag
  106. int offset , value;
  107. Array<int> theArray;
  108. while (Stop = false)
  109. {
  110. cout << "Enter an offset (0-9)";
  111. cout << "and a value. (-1 to stop): ";
  112. cin >> offset >>value;
  113.  
  114. if (offset < 0 )
  115. break;
  116. if (offset > 9)
  117. {
  118. cout << "*** Please use values between 0 and 9. ***\n";
  119. continue;
  120. }
  121. theArray[offset] = value;
  122. }
  123. cout << "\nHere's the entire array:\n";
  124. cout << theArray << endl;
  125.  
  126. return 0;
  127. }

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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,634
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1497
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: undefined reference error -HELP!

 
0
  #2
Dec 18th, 2006
My solution would be to write a public get function and use it in the loop instead of attempting to accessing the private member directly.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: undefined reference error -HELP!

 
1
  #3
Dec 18th, 2006
Originally Posted by JRM View Post
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!
I don't blame you, since what you encountered is one of the C++ gotchas and not many starters know about it.

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.
  1. template <class T>
  2. class Array ;
  3. template <class T>
  4. ostream& operator<< ( ostream&, Array<T>& ) ;
2. Place <> after the overloaded operator to let the compiler know that this is a templated funcion you are tryign to code here (syntactic sugar...bleh). Something like:
  1. template <class T> //declare the template and the parameter
  2. class Array // the class being paramterized
  3. {
  4. public:
  5. ....
  6. friend ostream& operator<< <>(ostream&, Array<T>&);
  7. private :
  8. ....
  9. };

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: undefined reference error -HELP!

 
0
  #4
Dec 24th, 2006
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...

  1. template <class T >
  2. class Array;
  3.  
  4. template <class T>
  5. ostream& operator << (ostream& , Array<T>& );
  6. template <class T> //declare the template and the parameter
  7. class Array // the class being paramterized
  8.  
  9.  
  10. {
  11. public:
  12. //costrucyors
  13. Array(int itsSize = DefaultSize);
  14. Array(const Array &rhs);
  15. ~Array() {delete [] ptype;}
  16.  
  17. //operators
  18. Array& operator = (const Array&);
  19. T& operator [] (int offset) {return ptype[offset];}
  20. const T& operator[] (int offset) const
  21. {return ptype[offset];}
  22. //accessors
  23. int GetSize() const {return itsSize;}
  24.  
  25. friend ostream& operator << <> (ostream&, Array<T>&);
  26.  
  27.  
  28.  
  29. private :
  30. T *ptype;
  31. int itsSize;
  32. };
  33.  
  34.  
  35. template <class T >
  36. ostream& operator << (ostream& output, Array<T>& theArray)
  37.  
  38.  
  39. {
  40. for (int i = 0; i < theArray.itsSize; i++)
  41. output << "[" << i << "]" << theArray[i] <<endl;
  42.  
  43. return output;
  44. }
Thanks for your input!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3753 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC