1,105,556 Community Members

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

Member Avatar
mir_sheely
Newbie Poster
15 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

#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.

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.

Member Avatar
mir_sheely
Newbie Poster
15 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,090 [?]
Q&As Helped to Solve: 347 [?]
Skill Endorsements: 8 [?]
 
0
 

You include stack.cpp before and after template definition ;)

Member Avatar
mir_sheely
Newbie Poster
15 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry thats by mistake..
No help after even removing one of them.

No difference either I place

#include "stack.cpp"

at the end of file or in the begining.

Member Avatar
Alex Edwards
Posting Shark
971 posts since Jun 2008
Reputation Points: 321 [?]
Q&As Helped to Solve: 109 [?]
Skill Endorsements: 2 [?]
 
0
 

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--

#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).

Member Avatar
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,090 [?]
Q&As Helped to Solve: 347 [?]
Skill Endorsements: 8 [?]
 
0
 

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...

Member Avatar
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 37 [?]
Q&As Helped to Solve: 69 [?]
Skill Endorsements: 0 [?]
 
0
 

Remove
'#include "stack.cpp"' at stack.h file..
Add
'#include "stack.h"'
to your stack.cpp file..

Member Avatar
mir_sheely
Newbie Poster
15 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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()

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>::Stack()'|
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?

GCC... TEsted on Dev Cpp and Code::Blocks on Vista. KDEVELOP on Opensuse Linux. same output.
My University requires compilation compatribility on GCC.

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 .

Member Avatar
mir_sheely
Newbie Poster
15 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

#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

Its Implementation.

#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

The LinkedList class Header.

#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

Its Implementation.

#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

Stack CLass Header file..

#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

Stack CLass Implementation.

#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.

Member Avatar
mir_sheely
Newbie Poster
15 posts since Apr 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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...

Member Avatar
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,090 [?]
Q&As Helped to Solve: 347 [?]
Skill Endorsements: 8 [?]
 
0
 

I have just compiled your original stack definition with MinGW compiler. It's a gcc 4.3.0 port. All template stuff was placed in one file.

However your previous post presents useless version of the package. It's impossible to compile Stack template specializations with stack.h header only.

Collect ALL Stack template stuff in stack.h (move member functions from stack.cpp to this file too) and try again.

Member Avatar
Fujur
Newbie Poster
1 post since Jan 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

Hello.

I had the same problem, and yes, if you copy your .cpp class specifications into the .h declaration the problem is solved.

An explanaition of why can be found here.

Good Luck.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article