template issues - need expert debugger!

Reply

Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

template issues - need expert debugger!

 
0
  #1
Jan 28th, 2005
hi there. i am on a UNIX system and am writing a template code called Array.h. it is called by a Test2.C file which does nothing more than declare a T object of type int. Array<int>.

i am getting all kinds of compile errors. have been over it and over it for about 8 hours now. can anyone help me here. i know there is something wrong with my non-member functions and i know there is something wrong with my init method that constructs my T's.

this is what i am getting from the compiler when i run Test2.C

Array.h:71: syntax error before `&' token
Array.h:71: `ostream' was not declared in this scope
Array.h:71: `os' was not declared in this scope
Array.h:71: syntax error before `<' token
Array.h:71: ISO C++ forbids declaration of `operator<<' with no type
Array.h:71: `int& operator<<(...)' must have an argument of class or enumerated
type
Array.h:71: `int& operator<<(...)' must take exactly two arguments
Array.h: In member function `void Array<T>::init(const T*, int)':
Array.h:349: syntax error before `;' token
Array.h: At global scope:
Array.h:366: syntax error before `>' token
Array.h:370: `b' was not declared in this scope
Array.h:370: syntax error before `;' token
Array.h:370: syntax error before `++' token

if anyone wanted to help me figure out what is going on with this mess that would be great. this is only PART A of what i need to be doing (ha ha).

and this is my Array.h of template T

  1. #include <assert.h>
  2. #include <iostream> // system files
  3.  
  4. #include "/unc/hedlund/121/lib/cpluslib.h" // comp 121 library
  5.  
  6. // GLOBAL CONSTANTS
  7. const int NOT_FOUND = -1; // returned when para not in array
  8.  
  9. template<class T>
  10. class Array
  11. {
  12.  
  13. protected:
  14. static const int DEF_SIZE = 100; // default array size
  15.  
  16.  
  17. public: // CONSTRUCTORS
  18. // default constructor
  19. // size = # of array elements
  20. // all elements initialized to 0
  21. Array ( int sz = DEF_SIZE );
  22.  
  23. // copy constructor
  24. // new array is a deep copy of x
  25. Array ( const Array<T>& x );
  26.  
  27. virtual ~Array(); // destructor
  28.  
  29.  
  30. // OBSERVER FUNCTIONS
  31. int size() const; // return # of array elements
  32.  
  33. // equality-identical in size and
  34. // contents of each array element?
  35. bool operator == ( const Array<T> &rhs ) const;
  36.  
  37. // inequality
  38. bool operator != ( const Array<T> &rhs ) const;
  39.  
  40. bool invariant () const; // is the object in a consistent state?
  41.  
  42. // MUTABLE FUNCTIONS
  43. // assignment-contents of RHS are copied
  44. // to LHS (self)
  45. Array<T>& operator = ( const Array<T> &rhs );
  46.  
  47. // index into array. No bounds checking.
  48. T& operator [] ( int i ) const;
  49.  
  50.  
  51. protected:
  52. // INTERNAL DATA REPRESENTATION
  53. int d_num_elements; // # of data elements
  54.  
  55. T* d_array; // ptr to first element of array
  56.  
  57.  
  58. private:
  59. // PRIVATE MEMBER FUNCTIONS
  60. [ Wrote 396 lines ]
  61.  
  62. classroom(52)% more Array.h
  63.  
  64. #include <assert.h>
  65. #include <iostream> // system files
  66.  
  67. #include "/unc/hedlund/121/lib/cpluslib.h" // comp 121 library
  68.  
  69. // GLOBAL CONSTANTS
  70. const int NOT_FOUND = -1; // returned when para not in array
  71.  
  72. template<class T>
  73. class Array
  74. {
  75.  
  76. protected:
  77. static const int DEF_SIZE = 100; // default array size
  78.  
  79.  
  80. public: // CONSTRUCTORS
  81. // default constructor
  82. // size = # of array elements
  83. // all elements initialized to 0
  84. Array ( int sz = DEF_SIZE );
  85.  
  86. // copy constructor
  87. // new array is a deep copy of x
  88. Array ( const Array<T>& x );
  89.  
  90. virtual ~Array(); // destructor
  91.  
  92.  
  93. // OBSERVER FUNCTIONS
  94. int size() const; // return # of array elements
  95.  
  96. // equality-identical in size and
  97. // contents of each array element?
  98. bool operator == ( const Array<T> &rhs ) const;
  99.  
  100. // inequality
  101. bool operator != ( const Array<T> &rhs ) const;
  102.  
  103. bool invariant () const; // is the object in a consistent state?
  104.  
  105. // MUTABLE FUNCTIONS
  106. // assignment-contents of RHS are copied
  107. // to LHS (self)
  108. Array<T>& operator = ( const Array<T> &rhs );
  109.  
  110. // index into array. No bounds checking.
  111. T& operator [] ( int i ) const;
  112.  
  113.  
  114. protected:
  115. // INTERNAL DATA REPRESENTATION
  116. int d_num_elements; // # of data elements
  117.  
  118. T* d_array; // ptr to first element of array
  119.  
  120.  
  121. private:
  122. // PRIVATE MEMBER FUNCTIONS
  123. // array initialization
  124. void init ( const T *x, int sz );
  125. };
  126.  
  127.  
  128. //
  129. // -------------------------NON-MEMBER FUNCTION PROTOTYPES-----------------------
  130. //
  131.  
  132. template<class T> // << displays array elements one per line
  133. ostream& operator << ( ostream& os, const Array<T> & b );
  134.  
  135. // return first position of v in
  136. template<class T> // array b or NOT_FOUND if v not in array
  137. int includes ( const Array<T> &b, int v);
  138.  
  139.  
  140. //
  141. //
  142. // ---------------------------------CONSTRUCTORS---------------------------------
  143. //
  144.  
  145. //
  146. // default constructor - initialize all elements to 0
  147. // Parameters
  148. // INBOUND - sz = # of elements in array
  149. // Pre - sufficient free memeory available AND ( 0 <= sz <= MAXINT)
  150. // Post - creates a new array of sz elements AND sz == size()
  151. // Runtime - 0(n)
  152. //
  153. template<class T>
  154. Array<T>::
  155. Array ( int sz )
  156. {
  157. // pre-condition assertion
  158. assert ( 0 <= sz <= MAXINT );
  159. cout << "!!! IntArray default constructor called" <<endl;
  160.  
  161. // init() called to initialize d_array
  162. // to 0 and d_num_elements to
  163. // to DEF_SIZE or sz
  164. init ( T(), sz );
  165. }
  166.  
  167.  
  168. //
  169. // copy constructor - make a deep copy of x
  170. // Parameters
  171. // INBOUND - x = array to copy
  172. // Pre - sufficient free memeory available
  173. // Post - creates a new array of x.size() elements AND
  174. // AND self == x AND old_self is deleted
  175. // Runtime - 0(n)
  176. //
  177. template<class T>
  178. Array<T>::
  179. Array ( const Array<T>& x )
  180. {
  181. cout << "!!! IntArray copy constructor called" <<endl;
  182.  
  183. // init() called to initialize d_array
  184. // to x.d_array and d_num_elements
  185. // to x.size()
  186. init ( x.d_array, x.size() );
  187. }
  188.  
  189.  
  190. //
  191. // destructor - releases memory used
  192. // Pre - TRUE
  193. // Post - reclaim memory used for
  194. // Runtime - 0(1)
  195. //
  196. template<class T>
  197. Array<T>::
  198. ~Array()
  199. {
  200. // delete entire array d_array
  201. cout << "!!! IntArray destructor called" <<endl;
  202. if ( size() > 0 ) {
  203. delete [] d_array;
  204. }
  205. }
  206.  
  207.  
  208. //
  209. //------------------------------OBSERVER FUNCTIONS-----------------------------
  210. //
  211.  
  212.  
  213. //
  214. // size - returns the # of elements in an array
  215. // Parameters
  216. // OUTBOUND - d_num_elements = number of elements in array
  217. // Pre - TRUE
  218. // Post - d_num_elements == size()
  219. // Runtime - 0(1)
  220. //
  221. template<class T>
  222. int Array<T>::
  223. size () const
  224. {
  225. // size of d_array is d_num_elements
  226. return (d_num_elements);
  227.  
  228. }
  229.  
  230.  
  231. //
  232. // equality - are the 2 arrays identical? They must match in size and
  233. // contents of each array element
  234. // Parameters
  235. // INBOUND - rhs = RHS of equality
  236. // OUTBOUND - boolean true or false
  237. // Pre - TRUE
  238. // Post - if lhs == rhs return TRUE, else return FALSE
  239. // Runtime - 0(n) worst case
  240. //
  241. template<class T>
  242. bool Array<T>::
  243. operator == ( const Array<T> &rhs ) const
  244. {
  245. if ( size() != rhs.size() ) { // check for same # elements in
  246. return ( FALSE ); // lhs array and rhs array
  247. }
  248. // if number of elements are equal
  249. // check for the element contents of
  250. // lhs and rhs for equality
  251. int i = 0;
  252. cout<< "checking for equality" <<endl;
  253. while ( i < size() ){
  254. // For all j: 0 .. i-1, self[j] == b[j]
  255. if ( self[i] != rhs[i] ){
  256. return ( FALSE );
  257. }
  258. else i++;
  259. }
  260. return ( TRUE );
  261. }
  262.  
  263.  
  264. //
  265. // inequality - are the 2 arrays different? They can differ in size
  266. // or contents of any array element
  267. // Parameters
  268. // INBOUND - rhs = RHS of inequality
  269. // OUTBOUND - boolean true or false
  270. // Pre - TRUE
  271. // Post - if lhs != ths then return TRUE, else return FALSE
  272. // Runtime - 0(n) worst case
  273. //
  274. template<class T>
  275. bool Array<T>::
  276. operator != ( const Array<T> &rhs) const
  277. {
  278. if ( size() == rhs.size() ) { // check for same # elements
  279. return ( FALSE ); // in lhs array and rhs array
  280. }
  281. // if number of elements are not
  282. // equal check the elements of
  283. // lhs and rhs for inequality
  284.  
  285. int i = 0;
  286. cout<< "checking for inequality" <<endl;
  287. while ( i < size() ){
  288. // For all j: 0 .. i-1, self[j] != b[j]
  289. if ( self[i] == rhs[i] ){
  290. return ( TRUE );
  291. }
  292. else i++;
  293. }
  294. return ( FALSE );
  295. }
  296.  
  297.  
  298. //
  299. // invariant - is the object in a consistent state?
  300. // Parameters - OUTBOUND - bool TRUE or FALSE
  301. // Pre - TRUE
  302. // Post - return TRUE if object is consistent, else return FALSE
  303. // Runtime - 0(1)
  304. //
  305. template<class T>
  306. bool Array<T>::
  307. invariant() const
  308. {
  309. bool inv1 = (TRUE); // d_array doesn't point to NULL
  310. if ( d_array == NULL){ // i.e. d_array has been initialized
  311. inv1 = (FALSE);
  312. }
  313.  
  314. bool inv2 = (TRUE); // d_array is equal to itself
  315. if ( d_array != d_array ){
  316. inv2 = (FALSE);
  317. }
  318.  
  319. bool inv3 = (TRUE); // d_num_elements is equal to
  320. if ( d_num_elements != size() ){ // size()
  321. inv3 = (FALSE);
  322. }
  323. // return true if all conditions
  324. // are true, else return false
  325. return ( inv1 && inv2 && inv3);
  326. }
  327.  
  328.  
  329. //
  330. //------------------------------MUTABLE FUNCTIONS----------------------------
  331. //
  332.  
  333.  
  334. //
  335. // assignment - make a deep copy of rhs into lhs
  336. // Parameters
  337. // INBOUND - rhs = RHS of assignment
  338. // OUTBOUND - new LHS object (deep copy of RHS)
  339. // Pre - TRUE
  340. // Post - self == rhs AND old_self is deleted
  341. // Runtime - 0(n)
  342. //
  343. template<class T>
  344. Array<T> & Array<T>::
  345. operator = ( const Array<T> &rhs )
  346. {
  347. if ( this == &rhs ){ // check for assignment to itself
  348. return ( self );
  349. }
  350. // free existing memory of lhs
  351. if (size() > 0 ){
  352. delete [] d_array;
  353. }
  354. init ( rhs.d_array, rhs.size() ); // initialize copy of rhs
  355. assert ( invariant() ); // check that new object is valid
  356. return ( self );
  357. }
  358.  
  359.  
  360. //
  361. // indexing - index into array. No bounds checking
  362. // Parameters -
  363. // INBOUND - int i = index #
  364. // OUTBOUND - object of type T
  365. // Pre - TRUE (since no bounds checking)
  366. // Post - object of type T in index i returned
  367. // Runtime - 0(n)
  368. //
  369. template<class T>
  370. T & Array<T>::
  371. operator [] ( int i ) const
  372. {
  373. if ( d_array == NULL ){
  374. return( NOT_FOUND );
  375. }
  376. else{
  377. return d_array[i];
  378. }
  379. }
  380.  
  381.  
  382. //
  383. //--------------------------PRIVATE MEMBER FUNCTIONS----------------------------
  384. //
  385.  
  386.  
  387. // init - create and initialize a new array. If an array is passed as the first
  388. // parameter
  389.  
  390.  
  391.  
  392. template<class T>
  393. void Array<T>::
  394. init ( const T *data, int sz )
  395. {
  396.  
  397. assert ( (0 <= sz) && (sz <= MAXINT));
  398.  
  399. d_num_elements = sz;
  400. if ( 0 == d_num_elements ){
  401. d_array = NULL;
  402. }
  403. else{
  404. d_array = new T [d_num_elements];
  405.  
  406. assert ( NULL != d_array );
  407. }
  408.  
  409. for (int i = 0; i < sz; i++ ){
  410. if ( NULL == data[i] ){
  411. self [i] = T;
  412. }
  413. else{
  414. self[i] = data[i];
  415. }
  416. }
  417. assert ( invariant() );
  418. }
  419.  
  420.  
  421. //
  422. //-----------------------------NON- MEMBER FUNCTIONS----------------------------
  423. //
  424.  
  425.  
  426. // << - display array elements one per line
  427.  
  428. template<classT>
  429. ostream &
  430. operator << ( ostream & os, const Array<T> & b )
  431. {
  432. for ( int i = 0; i < b.size(); i++ ){
  433.  
  434. os << b[i] << endl;
  435. }
  436. return ( os );
  437. }
  438.  
  439.  
  440. // includes - returns position of first v in b
  441.  
  442. template<class T>
  443. int
  444. includes ( const Array<T>& b, int v)
  445. {
  446. int i = 0;
  447.  
  448. while ( i <b.size() ){
  449. if ( b[i] == v ){
  450. return i;
  451. }
  452. else{
  453. i++
  454. }
  455. }
  456. return (NOT_FOUND);
  457. }

and this is my Test2.C

  1. int
  2. main()
  3. {
  4.  
  5.  
  6.  
  7.  
  8. cout<< "*******Declaration of 3 IntArray objects/size() checking******* \n" <<endl;
  9.  
  10. cout<< "IntArray a declared with no parameters" <<endl;
  11.  
  12. Array<int> a;
  13. cout << " " <<endl;
  14.  
  15. return 0;
  16. }

thanks
crq
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: template issues - need expert debugger!

 
0
  #2
Jan 28th, 2005
First, place this after your includes:
  1. using namespace std;
Then look for typos. Recompile and we'll go from there.

p.s. It couldn't hurt to read my reply to your other thread. Notice how I used T(), not T, for default construction of a value of template parameter type.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: template issues - need expert debugger!

 
0
  #3
Jan 29th, 2005
thanks for looking at this ...

this is Test2.C

  1.  
  2. #include <assert.h>
  3. #include <iostream>
  4. #include "Array.h"
  5.  
  6. #include "/unc/hedlund/121/lib/cpluslib.h"
  7.  
  8. using namespace std;
  9.  
  10. int
  11. main()
  12. {
  13.  
  14.  
  15.  
  16.  
  17. cout<< "*******Declaration of 3 IntArray objects/size() checking******* \n" <<endl;
  18.  
  19. cout<< "IntArray a declared with no parameters" <<endl;
  20.  
  21. Array<int> a;
  22. cout << " " <<endl;
  23.  
  24. return 0;
  25. }

and the Array.h is the same except for putting the

using namespace std;

after the includes and putting the () after the T in the init method.

and this is what the compiler says ...

hmmm... syntax error. that is usually a goof error. are the numbers at the side line numbers? anyone know how to turn on the line numbers in pico?

In file included from Test2.C:4:
Array.h:367: syntax error before `>' token
Array.h:371: `b' was not declared in this scope
Array.h:371: syntax error before `;' token
Array.h:371: syntax error before `++' token
Array.h:384: too many template parameter lists in declaration of `int
includes(const Array<T>&, int)'
Array.h:384: syntax error before `{' token
Test2.C:12: cannot declare `::main' to be a template
Test2.C:12: confused by earlier errors, bailing out

thanks for looking at this!! ... i appreciate it more than you can ever know. really.

crq
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: template issues - need expert debugger!

 
0
  #4
Jan 29th, 2005
Look, everytime you use a standard C++ name (like ostream), it needs to be qualified by std::, or have a using declaration or using directive before you use it. I was trying to give you the most expedient solution, but you didn't do it right. Generally, std:: is used for headers, and for source files you can do what you want:
  1. // class.h
  2. #include <iostream>
  3.  
  4. class C {
  5. // Stuff
  6. friend std::ostream& operator<<(ostream& out, const C& c);
  7. friend std::istream& operator>>(istream& in, C& c);
  8. };
  1. // class.cpp
  2. #include "class.h"
  3.  
  4. using std::ostream;
  5. using std::istream;
  6.  
  7. ostream& operator<<(ostream& out, const C& c)
  8. {
  9. // Implementation
  10. }
  11.  
  12. istream& operator>>(istream& in, C& c)
  13. {
  14. // Implementation
  15. }
  1. // main.cpp
  2. #include <iostream>
  3. #include "class.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. // Stuff
  10. }
Your primary problem is that you failed to do this properly, so the compiler doesn't recognize unqualified names from the std namespace. Once you fix this, the real errors will be easier to find.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: template issues - need expert debugger!

 
0
  #5
Jan 29th, 2005
[code]
// class.h
#include <iostream>

class C {
// Stuff
friend std::ostream& operator<<(ostream& out, const C& c);
friend std::istream& operator>>(istream& in, C& c);
};

i tried this in my Array.h template class file. but i am supposed to use ostream overloading in a non-member function. so the friend wasn't accepted by my compiler. that's why i am having trouble figuring this out. template/non-member functions/and ostream overloading is a bit out of my league at this point.

thanks
crq
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: template issues - need expert debugger!

 
0
  #6
Jan 29th, 2005
Originally Posted by crq
[code]
// class.h
#include <iostream>

class C {
// Stuff
friend std::ostream& operator<<(ostream& out, const C& c);
friend std::istream& operator>>(istream& in, C& c);
};

i tried this in my Array.h template class file. but i am supposed to use ostream overloading in a non-member function. so the friend wasn't accepted by my compiler. that's why i am having trouble figuring this out. template/non-member functions/and ostream overloading is a bit out of my league at this point.

thanks
crq


sorry, i don't know how i put that little wierd face in the code ...

crq
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: template issues - need expert debugger!

 
0
  #7
Jan 29th, 2005
>but i am supposed to use ostream overloading in a non-member function.
Friend functions are not member functions. They are non-member functions that have access to the private members of the class.

>so the friend wasn't accepted by my compiler
Well, I have no idea what's wrong with your code because you use a stupid non-standard header file that declares equally stupid things. Since I don't have the header, I can't compile your code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 24
Reputation: crq is an unknown quantity at this point 
Solved Threads: 0
crq crq is offline Offline
Newbie Poster

Re: template issues - need expert debugger!

 
0
  #8
Jan 29th, 2005
Originally Posted by Narue
>but i am supposed to use ostream overloading in a non-member function.
Friend functions are not member functions. They are non-member functions that have access to the private members of the class.

>so the friend wasn't accepted by my compiler
Well, I have no idea what's wrong with your code because you use a stupid non-standard header file that declares equally stupid things. Since I don't have the header, I can't compile your code.
i am using the skeleton that my instructor provided. i am not very excited about the way he has laid this stuff out either. i am just trying to stay within the rules that he has set for me. but i won't bother asking anymore questions on here. you seem to enjoy the saying nasty things to me ... but i guess you only do that to us "stupid" people.

thanks
crq
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: template issues - need expert debugger!

 
0
  #9
Jan 29th, 2005
>i am just trying to stay within the rules that he has set for me.
Posting code that uses declarations in a header that you don't provide isn't very helpful. At the very least, you could post /unc/hedlund/121/lib/cpluslib.h so that I know what parts are due to a retarded instructor and what parts are actual errors.

>you seem to enjoy the saying nasty things to me
If I recall correctly, the only thing nasty I said was in reference to your instructor's header file. But if you don't want my help, as seems to be the case with people who quickly take offense at nothing, I won't offer it. Your loss.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC