error: expected constructor, destructor, or type conversion before '<' to

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 13
Reputation: mir_sheely is an unknown quantity at this point 
Solved Threads: 0
mir_sheely mir_sheely is offline Offline
Newbie Poster

error: expected constructor, destructor, or type conversion before '<' to

 
0
  #1
Nov 16th, 2008
I am Making a STACK class which could hold a Binary Tree Nodes.
I have defined a class Node.
Then made a linkedlist classs.
and finally the stack class. All are Template based.
But during compilation it is giving me the following error.
error: expected constructor, destructor, or type conversion before '<' to

I am using GCC via KDEVELOP on SUSE 11.0

Here is stack.h
  1.  
  2. #ifndef STACK_H
  3.  
  4. #define STACK_H
  5.  
  6. #include "linkedlist.h"
  7.  
  8. #include <iostream>
  9. #include "stack.cpp"
  10.  
  11. using namespace std ;
  12. template <class T>
  13.  
  14. class stack
  15.  
  16. {
  17.  
  18. public:
  19.  
  20. stack();
  21.  
  22. ~stack();
  23.  
  24. void push(T c) ;
  25.  
  26. T pop() ;
  27.  
  28. bool is_empty() ;
  29.  
  30. T peek() ;
  31.  
  32. /*void display_stack() ;*/
  33.  
  34. private:
  35.  
  36. linkedlist<T> lst ;
  37.  
  38.  
  39.  
  40. };
  41.  
  42.  
  43. #include "stack.cpp"
  44. #endif

and here is its implementation.

  1. template <class T> stack<T>::stack()
  2.  
  3. {
  4.  
  5. }
  6.  
  7.  
  8.  
  9. template <class T> stack<T>::~stack()
  10.  
  11. {
  12.  
  13. }
  14.  
  15. template <class T> void stack<T>::push(T c)
  16.  
  17. {
  18.  
  19. lst.Insert(c) ;
  20.  
  21. }
  22.  
  23.  
  24.  
  25. template <class T> T stack<T>::pop()
  26.  
  27. {
  28.  
  29. T c = lst.get_head_suffix() ;
  30.  
  31. lst.Remove() ;
  32.  
  33. return c ;
  34.  
  35. }
  36.  
  37.  
  38.  
  39. template <class T> bool stack<T>::is_empty()
  40.  
  41. {
  42.  
  43. if (lst.get_head() == 0)
  44.  
  45. return true ;
  46.  
  47. else
  48.  
  49. return false ;
  50.  
  51. }
  52.  
  53.  
  54.  
  55. template <class T> T stack<T>::peek()
  56.  
  57. {
  58.  
  59. if (!is_empty())
  60.  
  61. {
  62.  
  63. T c = lst.get_head_suffix() ;
  64.  
  65. return c ;
  66.  
  67. }
  68.  
  69. }

Any help would be warmly welcomed so plzzz... help me.
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: mir_sheely is an unknown quantity at this point 
Solved Threads: 0
mir_sheely mir_sheely is offline Offline
Newbie Poster

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #2
Nov 16th, 2008
Here is complete list of Error...

stack.cpp:1: error: expected constructor, destructor, or type conversion before '<' token
stack.cpp:9: error: expected constructor, destructor, or type conversion before '<' token
stack.cpp:15: error: expected initializer before '<' token
stack.cpp:25: error: expected initializer before '<' token
stack.cpp:39: error: expected initializer before '<' token
stack.cpp:55: error: expected initializer before '<' to


I have adjusted the line Numbering respectively.
Last edited by mir_sheely; Nov 16th, 2008 at 4:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #3
Nov 16th, 2008
You include stack.cpp before and after template definition
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: mir_sheely is an unknown quantity at this point 
Solved Threads: 0
mir_sheely mir_sheely is offline Offline
Newbie Poster

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #4
Nov 17th, 2008
Sorry thats by mistake..
No help after even removing one of them.

No difference either I place
  1. #include "stack.cpp"
at the end of file or in the begining.
Last edited by mir_sheely; Nov 17th, 2008 at 1:56 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #5
Nov 17th, 2008
The problem may be that the compiler sees a possibility of your .cpp file attempt to define something that doesn't exist, since your header file is conditionally defined.

You can make it, such that, the text in your implementation file is read by the compiler if and only if the header is defined--

  1. #ifdef STACK_H
  2.  
  3. template <class T> stack<T>::stack()
  4.  
  5. {
  6.  
  7. }
  8.  
  9.  
  10.  
  11. template <class T> stack<T>::~stack()
  12.  
  13. {
  14.  
  15. }
  16.  
  17. template <class T> void stack<T>::push(T c)
  18.  
  19. {
  20.  
  21. lst.Insert(c) ;
  22.  
  23. }
  24.  
  25.  
  26.  
  27. template <class T> T stack<T>::pop()
  28.  
  29. {
  30.  
  31. T c = lst.get_head_suffix() ;
  32.  
  33. lst.Remove() ;
  34.  
  35. return c ;
  36.  
  37. }
  38.  
  39.  
  40.  
  41. template <class T> bool stack<T>::is_empty()
  42.  
  43. {
  44.  
  45. if (lst.get_head() == 0)
  46.  
  47. return true ;
  48.  
  49. else
  50.  
  51. return false ;
  52.  
  53. }
  54.  
  55.  
  56.  
  57. template <class T> T stack<T>::peek()
  58.  
  59. {
  60.  
  61. if (!is_empty())
  62.  
  63. {
  64.  
  65. T c = lst.get_head_suffix() ;
  66.  
  67. return c ;
  68.  
  69. }
  70.  
  71. }
  72.  
  73. #endif

-- you are not the only one with this problem. Apparently when using eager-inclusion, the implementation file must be restricted.

-Alex

Note: Hopefully you are also only including your Stack.h header file and not the .cpp file with it (since its already being included via eager inclusion).
Last edited by Alex Edwards; Nov 17th, 2008 at 2:29 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #6
Nov 17th, 2008
VC++ 2008 compiles this template w/o errors. Of course, I define dummy linklist template with get_head() to link test executable.
What compiler are you using?

It was not the best idea to call your own class "stack": it's a name of STL class. It's not an error, but better use Stack name or what else. Common convention: user class names are capitalized...
Last edited by ArkM; Nov 17th, 2008 at 4:47 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #7
Nov 17th, 2008
Remove
'#include "stack.cpp"' at stack.h file..
Add
'#include "stack.h"'
to your stack.cpp file..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: mir_sheely is an unknown quantity at this point 
Solved Threads: 0
mir_sheely mir_sheely is offline Offline
Newbie Poster

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #8
Nov 17th, 2008
Originally Posted by cikara21 View Post
Remove
'#include "stack.cpp"' at stack.h file..
Add
'#include "stack.h"'
to your stack.cpp file..
According to my readings, it is different for Templated class (FAQLITE.com mentions it and ask to use EXPORT keyword, but GCC, VCC doesn't implement export now.
So I included *.cpp in *.h instead of including *.h in .cpp.

If I follow the standard approach mentioned by you then the error is:

I have written following in main()

  1. int main()
  2. {
  3. Stack<char> s1 ;
  4. char s[4] ;
  5. s1.push('S') ;
  6. s1.push('A') ;
  7. s1.push('A') ;
  8. s1.push('Z') ;
  9. for (int i = 0 ; i<4; i++)
  10. s[i] = s1.pop() ;
  11. for (int i = 3 ; i>=0; i--)
  12. cout<<s[i]<<" " ;
  13. cout << "Hello world!" << endl;
  14. return 0;
  15. }

The Error is as follows.


undefined reference to `Stack<char>:tack()'|
undefined reference to `Stack<char>::push(char)'
undefined reference to `Stack<char>::push(char)'
undefined reference to `Stack<char>::push(char)'
undefined reference to `Stack<char>::push(char)'
undefined reference to `Stack<char>::pop()'|
undefined reference to `Stack<char>::~Stack()'
||=== Build finished: 8 errors, 0 warnings ===|


Originally Posted by Arkm View Post
VC++ 2008 compiles this template w/o errors. Of course, I define dummy linklist template with get_head() to link test executable.
What compiler are you using?
GCC... TEsted on Dev Cpp and Code::Blocks on Vista. KDEVELOP on Opensuse Linux. same output.
My University requires compilation compatribility on GCC.

Originally Posted by Arkm View Post
It was not the best idea to call your own class "stack": it's a name of STL class. It's not an error, but better use Stack name or what else. Common convention: user class names are capitalized...
Changed to Stack.. even removed using namespace std; but no luck .
Last edited by mir_sheely; Nov 17th, 2008 at 11:18 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: mir_sheely is an unknown quantity at this point 
Solved Threads: 0
mir_sheely mir_sheely is offline Offline
Newbie Poster

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #9
Nov 17th, 2008
HEre all contents of my project.
My project conatins 6 files, 3 .cpp and 3 .h

first the basic class node.h as unit of linkedlist.

  1. #ifndef NODE_H
  2. #define NODE_H
  3. #include <iostream>
  4. template <class T>
  5. class node
  6. {
  7. public:
  8. node();
  9. ~node();
  10. node(T c, node *n = 0) ;
  11. void set_suffix(T c) ;
  12. T get_suffix() ;
  13. void set_next(node<T> * n) ;
  14. node<T>* get_next() ;
  15.  
  16. private:
  17. T suffix;
  18. node<T>* next;
  19. };
  20. #endif // NODE_H
Its Implementation.
  1. #include "node.h"
  2. #ifdef STACK_H
  3. template <class T>
  4. node<T>::node()
  5. {
  6. suffix = 0 ;
  7. next = 0 ;
  8. }
  9. template <class T>
  10. node<T>::node(T c, node *n)
  11. {
  12. suffix = c ;
  13. next = n ;
  14. }
  15.  
  16. template <class T>
  17. node<T>::~node()
  18. {
  19. }
  20. template <class T>
  21. void node<T>::set_suffix(T c)
  22. {
  23. suffix = c ;
  24. }
  25.  
  26. template <class T>
  27. T node<T>::get_suffix()
  28. {
  29. return suffix ;
  30. }
  31.  
  32. template <class T>
  33. void node<T>::set_next(node<T> * n)
  34. {
  35. next = n ;
  36. }
  37.  
  38. template <class T>
  39. node<T>* node<T>::get_next()
  40. {
  41. return next ;
  42. }
  43. #endif
The LinkedList class Header.
  1. #ifndef LINKEDLIST_H
  2. #define LINKEDLIST_H
  3. #include "node.h"
  4. template <class T>
  5. class linkedlist
  6. {
  7. public:
  8. linkedlist();
  9. ~linkedlist();
  10. linkedlist(const linkedlist<T> & rhs) ;
  11. linkedlist& operator = (const linkedlist<T>& rhs) ;
  12. void delete_List() ;
  13. void Insert(T c) ;
  14. void Remove() ;
  15. T get_head_suffix() ;
  16. bool not_empty() ;
  17. //void Display_List() ;
  18.  
  19. private:
  20. node<T> * head ;
  21. };
  22. #endif // LINKEDLIST_H
Its Implementation.
  1. #include "linkedlist.h"
  2. #ifdef STACK_H
  3. template <class T>
  4. linkedlist<T>::linkedlist()
  5. {
  6. head = 0 ;
  7. }
  8.  
  9. template <class T>
  10. linkedlist<T>::linkedlist(const linkedlist<T> & rhs)
  11. {
  12. node<T>* temp = rhs.head, *temp2 = head, *prev_node ;
  13. while (temp!= 0)
  14. {
  15. temp2 = new node<T>(temp2->get_suffix()) ;
  16. if (head == 0)
  17. {
  18. head = temp2 ;
  19. prev_node = head ;
  20. }
  21. else
  22. {
  23. prev_node->set_next(temp2) ;
  24. prev_node = temp2 ;
  25. }
  26. temp = temp->get_next() ;
  27. }
  28. }
  29.  
  30. template <class T>
  31. void linkedlist<T>::delete_List()
  32. {
  33. node<T> *temp = head, *temp2 = head ;
  34. head = 0 ;
  35. while(temp != 0)
  36. {
  37. temp = temp->get_next() ;
  38. delete temp2 ;
  39. temp2 = temp ;
  40. }
  41. }
  42.  
  43.  
  44. template <class T>
  45. linkedlist<T>::~linkedlist()
  46. {
  47. delete_List() ;
  48. }
  49.  
  50. template <class T>
  51. linkedlist<T>& linkedlist<T>::operator = (const linkedlist<T>& rhs)
  52. {
  53. if (this != &rhs)
  54. {
  55. node<T>* temp = rhs.head, *temp2 = head, *prev_node ;
  56. while (temp!= 0)
  57. {
  58. temp2 = new node<T>(temp2->get_suffix()) ;
  59. if (head == 0)
  60. {
  61. head = temp2 ;
  62. prev_node = head ;
  63. }
  64. else
  65. {
  66. prev_node->set_next(temp2) ;
  67. prev_node = temp2 ;
  68. }
  69. temp = temp->get_next() ;
  70. }
  71. }
  72. return *this;
  73. }
  74.  
  75. template <class T>
  76. void linkedlist<T>::Insert(T c)
  77. {
  78. node<T>* temp ;
  79. temp = new node<T>(c) ;
  80. if (head == 0)
  81. head = temp ;
  82. else
  83. {
  84. temp->set_next(head) ;
  85. head = temp ;
  86. }
  87. }
  88.  
  89. template <class T>
  90. void linkedlist<T>::Remove()
  91. {
  92. if (head == 0)
  93. return ;
  94. node<T>* temp=head ;
  95. head = temp->get_next() ;
  96. delete temp ;
  97. return ;
  98. }
  99.  
  100. template <class T>
  101. T linkedlist<T>::get_head_suffix()
  102. {
  103. return head->get_suffix() ;
  104. }
  105.  
  106. template <class T>
  107. bool linkedlist<T>::not_empty()
  108. {
  109. if (head == 0)
  110. return true ;
  111. else
  112. return false ;
  113. }
  114. #endif
Stack CLass Header file..
  1. #ifndef STACK_H
  2. #define STACK_H
  3. #include "linkedlist.h"
  4. template <class T>
  5. class Stack
  6. {
  7. public:
  8. Stack();
  9. ~Stack();
  10. void push(T c) ;
  11. T pop() ;
  12. bool is_empty() ;
  13. T peek() ;
  14. void display_Stack() ;
  15. private:
  16. linkedlist<T> lst ;
  17.  
  18. };
  19. #endif
Stack CLass Implementation.
  1. #include "stack.h"
  2. #ifdef STACK_H
  3. template <class T>
  4. Stack<T>::Stack()
  5. {
  6. }
  7.  
  8. template <class T>
  9. void Stack<T>::push(T c)
  10. {
  11. lst.Insert(c) ;
  12. }
  13.  
  14. template <class T>
  15. T Stack<T>::pop()
  16. {
  17. T c = lst.get_head_suffix() ;
  18. lst.Remove() ;
  19. return c ;
  20. }
  21.  
  22. template <class T>
  23. bool Stack<T>::is_empty()
  24. {
  25. if (lst.get_head() == 0)
  26. return true ;
  27. else
  28. return false ;
  29. }
  30.  
  31. template <class T>
  32. T Stack<T>::peek()
  33. {
  34. if (!is_empty())
  35. {
  36. T c = lst.get_head_suffix() ;
  37. return c ;
  38. }
  39. }
  40.  
  41. template <class T>
  42. void Stack<T>::display_Stack()
  43. {
  44. lst.Display_List() ;
  45. }
  46.  
  47. #endif

Included "stack.h" in ain and what I am doing in main I demonstrated it earlier..

Plz try ur best.. little days are remaining for the submission and I have to use this stack for Expression trees.. also would create a Queue class with same linkedlist to do Huffman Encoding and Decoding.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: mir_sheely is an unknown quantity at this point 
Solved Threads: 0
mir_sheely mir_sheely is offline Offline
Newbie Poster

Re: error: expected constructor, destructor, or type conversion before '<' to

 
0
  #10
Nov 17th, 2008
One thing more. If I implement the implementation as same in the declaration file and save it as .cpp and then include stack.cpp in my main it works...
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC