| | |
error: expected constructor, destructor, or type conversion before '<' to
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
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
and here is its implementation.
Any help would be warmly welcomed so plzzz... help me.
Thanks.
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
C++ Syntax (Toggle Plain Text)
#ifndef STACK_H #define STACK_H #include "linkedlist.h" #include <iostream> #include "stack.cpp" using namespace std ; template <class T> class stack { public: stack(); ~stack(); void push(T c) ; T pop() ; bool is_empty() ; T peek() ; /*void display_stack() ;*/ private: linkedlist<T> lst ; }; #include "stack.cpp" #endif
and here is its implementation.
C++ Syntax (Toggle Plain Text)
template <class T> stack<T>::stack() { } template <class T> stack<T>::~stack() { } template <class T> void stack<T>::push(T c) { lst.Insert(c) ; } template <class T> T stack<T>::pop() { T c = lst.get_head_suffix() ; lst.Remove() ; return c ; } template <class T> bool stack<T>::is_empty() { if (lst.get_head() == 0) return true ; else return false ; } template <class T> T stack<T>::peek() { if (!is_empty()) { T c = lst.get_head_suffix() ; return c ; } }
Any help would be warmly welcomed so plzzz... help me.
Thanks.
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
Sorry thats by mistake..
No help after even removing one of them.
No difference either I place
at the end of file or in the begining.
No help after even removing one of them.
No difference either I place
c++ Syntax (Toggle Plain Text)
#include "stack.cpp"
Last edited by mir_sheely; Nov 17th, 2008 at 1:56 am.
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--
-- 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).
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--
c++ Syntax (Toggle Plain Text)
#ifdef STACK_H template <class T> stack<T>::stack() { } template <class T> stack<T>::~stack() { } template <class T> void stack<T>::push(T c) { lst.Insert(c) ; } template <class T> T stack<T>::pop() { T c = lst.get_head_suffix() ; lst.Remove() ; return c ; } template <class T> bool stack<T>::is_empty() { if (lst.get_head() == 0) return true ; else return false ; } template <class T> T stack<T>::peek() { if (!is_empty()) { T c = lst.get_head_suffix() ; return c ; } } #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.
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...
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.
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
Remove
'#include "stack.cpp"' at stack.h file..
Add
'#include "stack.h"'
to your stack.cpp file..
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()
C++ Syntax (Toggle Plain Text)
int main() { Stack<char> s1 ; char s[4] ; s1.push('S') ; s1.push('A') ; s1.push('A') ; s1.push('Z') ; for (int i = 0 ; i<4; i++) s[i] = s1.pop() ; for (int i = 3 ; i>=0; i--) cout<<s[i]<<" " ; cout << "Hello world!" << endl; return 0; }
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 ===|
•
•
•
•
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?
My University requires compilation compatribility on GCC.
Changed to Stack.. even removed using namespace std; but no luck .
Last edited by mir_sheely; Nov 17th, 2008 at 11:18 am.
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
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.
Its Implementation.
The LinkedList class Header.
Its Implementation.
Stack CLass Header file..
Stack CLass Implementation.
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.
My project conatins 6 files, 3 .cpp and 3 .h
first the basic class node.h as unit of linkedlist.
C++ Syntax (Toggle Plain Text)
#ifndef NODE_H #define NODE_H #include <iostream> template <class T> class node { public: node(); ~node(); node(T c, node *n = 0) ; void set_suffix(T c) ; T get_suffix() ; void set_next(node<T> * n) ; node<T>* get_next() ; private: T suffix; node<T>* next; }; #endif // NODE_H
C++ Syntax (Toggle Plain Text)
#include "node.h" #ifdef STACK_H template <class T> node<T>::node() { suffix = 0 ; next = 0 ; } template <class T> node<T>::node(T c, node *n) { suffix = c ; next = n ; } template <class T> node<T>::~node() { } template <class T> void node<T>::set_suffix(T c) { suffix = c ; } template <class T> T node<T>::get_suffix() { return suffix ; } template <class T> void node<T>::set_next(node<T> * n) { next = n ; } template <class T> node<T>* node<T>::get_next() { return next ; } #endif
C++ Syntax (Toggle Plain Text)
#ifndef LINKEDLIST_H #define LINKEDLIST_H #include "node.h" template <class T> class linkedlist { public: linkedlist(); ~linkedlist(); linkedlist(const linkedlist<T> & rhs) ; linkedlist& operator = (const linkedlist<T>& rhs) ; void delete_List() ; void Insert(T c) ; void Remove() ; T get_head_suffix() ; bool not_empty() ; //void Display_List() ; private: node<T> * head ; }; #endif // LINKEDLIST_H
C++ Syntax (Toggle Plain Text)
#include "linkedlist.h" #ifdef STACK_H template <class T> linkedlist<T>::linkedlist() { head = 0 ; } template <class T> linkedlist<T>::linkedlist(const linkedlist<T> & rhs) { node<T>* temp = rhs.head, *temp2 = head, *prev_node ; while (temp!= 0) { temp2 = new node<T>(temp2->get_suffix()) ; if (head == 0) { head = temp2 ; prev_node = head ; } else { prev_node->set_next(temp2) ; prev_node = temp2 ; } temp = temp->get_next() ; } } template <class T> void linkedlist<T>::delete_List() { node<T> *temp = head, *temp2 = head ; head = 0 ; while(temp != 0) { temp = temp->get_next() ; delete temp2 ; temp2 = temp ; } } template <class T> linkedlist<T>::~linkedlist() { delete_List() ; } template <class T> linkedlist<T>& linkedlist<T>::operator = (const linkedlist<T>& rhs) { if (this != &rhs) { node<T>* temp = rhs.head, *temp2 = head, *prev_node ; while (temp!= 0) { temp2 = new node<T>(temp2->get_suffix()) ; if (head == 0) { head = temp2 ; prev_node = head ; } else { prev_node->set_next(temp2) ; prev_node = temp2 ; } temp = temp->get_next() ; } } return *this; } template <class T> void linkedlist<T>::Insert(T c) { node<T>* temp ; temp = new node<T>(c) ; if (head == 0) head = temp ; else { temp->set_next(head) ; head = temp ; } } template <class T> void linkedlist<T>::Remove() { if (head == 0) return ; node<T>* temp=head ; head = temp->get_next() ; delete temp ; return ; } template <class T> T linkedlist<T>::get_head_suffix() { return head->get_suffix() ; } template <class T> bool linkedlist<T>::not_empty() { if (head == 0) return true ; else return false ; } #endif
C++ Syntax (Toggle Plain Text)
#ifndef STACK_H #define STACK_H #include "linkedlist.h" template <class T> class Stack { public: Stack(); ~Stack(); void push(T c) ; T pop() ; bool is_empty() ; T peek() ; void display_Stack() ; private: linkedlist<T> lst ; }; #endif
C++ Syntax (Toggle Plain Text)
#include "stack.h" #ifdef STACK_H template <class T> Stack<T>::Stack() { } template <class T> void Stack<T>::push(T c) { lst.Insert(c) ; } template <class T> T Stack<T>::pop() { T c = lst.get_head_suffix() ; lst.Remove() ; return c ; } template <class T> bool Stack<T>::is_empty() { if (lst.get_head() == 0) return true ; else return false ; } template <class T> T Stack<T>::peek() { if (!is_empty()) { T c = lst.get_head_suffix() ; return c ; } } template <class T> void Stack<T>::display_Stack() { lst.Display_List() ; } #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.
![]() |
Similar Threads
- so I find myself here again (C++)
- More help (C++)
- Class Template used for stack (C++)
- Matrix Determinate Calculation (C++)
- what's wrong with this code ? (C++)
- expected init-declarator (C++)
- What is the best value for an empty point in C++? (C++)
- g++ allegro linking (C++)
- Stupid question(using class object) (C++)
- Grrr C++ Help Me! (C++)
Other Threads in the C++ Forum
- Previous Thread: Problem with OOP and classes...
- Next Thread: error dynamic_cast in c++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






