having trouble tracking down a particular C++ error

Hello! This will probably be a very simple error, but I can't find it to save my life.

The error i'm getting: Stack.h:79: error: expected initializer before ‘&’ token

Here is my stack.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef STACK_H
#define STACK_H

#include "StackException.h"

const int START_SIZE = 10;


template <typename T>
class Stack : public Stack<T>
{
private:
	bool empty;
	int count;
	int capacity ;
	T* items;
public:
	Stack();
	Stack(T data);
	Stack(const Stack &s);
	const Stack& operator=(const Stack &s);
	~Stack();
	void push(T data);
	T pop() throw(StackException);
	T top() throw(StackException);
	bool isEmpty();
	void grow();
};

template <typename T>
Stack<T>::Stack()
{
	empty = 1;
	count = 0;
	capacity = START_SIZE;
	items = new T [START_SIZE];
}

template <typename T>
Stack<T>::Stack(T data)
{
	empty = 0;
	count = 1;
	capacity = START_SIZE;
	items = new T [START_SIZE];
	push(data);
}

template <typename T>
Stack<T>::Stack(const Stack<T> & s)
{
	this->empty = s.empty;
	this->count = s.count;
	this->capacity = s.capacity;

	this->items = new T [s.capacity];

	for(int i = 0; i < s.capacity; i++)
	{
		this->items[i] = s.items[i];
	}

}

template <typename T>
Stack<T>::~Stack()
{
	delete [] this->items;
	this->items = NULL;
}

template <typename T>
bool Stack<T>::isEmpty()
{
	return empty;
}

template <typename T>
const Stack& Stack<T>::operator=(const Stack<T> &s) //error is here
{
	this->items = new T [s.capacity];
	for(int i = 0; i < s.capacity; i++)
	{
		this->items[i] = s.items[i];
	}
	this->capacity = s.capacity;
	this->count = s.count;
	this->empty = s.empty;
}

template <typename T>
T Stack<T>::pop() throw(StackException)
{
	T index;
	if(empty)
	{
		throw StackException();
	}
	else
	{
		if(count = 1)
		{
			empty = 1;
		}
		
	}

	count--;
	index = items[count - 1];
	items[count - 1] = 0;
	return(index);
}

template <typename T>
void Stacks<T>::push (T data)
{
	if (count == capacity)
	{
		grow();
	}
	items[count] = data;
	empty = 0;
	count ++;
}

template <typename T>
T Stack<T>::top() throw(StackException)
{
	return(items[count-1];
}

template <typename T>
void Stack<T>::grow()
{
	T* temp;
	T *previous;

	previous = items;
	temp = new T[capacity * 2];
	for(int i = 0; i < count; i++)
	{
		temp[i] = previous[i];
	}
	items = temp;
	temp = NULL;
	delete[] previous;
	previous = NULL;
	capacity *= 2;
}


#endif 


From what I've found online, this is often caused by a misplaced semi-colon, but I'm fairly sure I haven't missed one. Thanks for your time!
clang says:

test.cc:76:7: error: use of class template Stack requires template arguments
const Stack& Stack<T>::operator=(const Stack<T> &s) //error is here
      ^
test.cc:7:7: note: template is declared here
class Stack : public Stack<T>
      ^


You wrote that your opreator= is returning something called "const Stack&", but there is no such type: Stack is a template, it needs something between angle brackets to become a type, such as "const Stack<T>&"

Note also

test.cc:98:12: warning: using the result of an assignment as a condition without
      parentheses [-Wparentheses]
                if(count = 1)
                   ~~~~~~^~~
test.cc:98:12: note: place parentheses around the assignment to silence this
      warning
                if(count = 1)
                         ^
                   (        )
test.cc:98:12: note: use '==' to turn this assignment into an equality
      comparison
                if(count = 1)
                         ^
                         ==

and
test.cc:112:6: error: no template named 'Stacks'; did you mean 'Stack'?
void Stacks<T>::push (T data)
     ^~~~~~
     Stack

and
test.cc:126:23: error: expected ')'
        return(items[count-1];
                             ^
test.cc:126:8: note: to match this '('
        return(items[count-1];
              ^


not to mention that "class Stack : public Stack<T>" seems to be an error to begin with: you're apparently trying to define "template<typename T> class Stack". With your template definition, you will see a compile-time error when you attempt to instantiate a concrete Stack<int>, for example.
Last edited on
Oh wow, thanks!

So, I fixed all the syntax errors you mentioned. I changed the first block of code into:

const Stack<T>& Stack<T>::operator=(const Stack<T> &s)

That fixed it.

Unfortunately, you're right; I am getting an error when i try to instantiate a Stack<int>. Would declaring the class as:

 
template<typename T> class Stack<T>


fix the probem?


Here is my revised code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef STACK_H
#define STACK_H

#include "StackException.h"

const int START_SIZE = 10;


template <typename T>
class Stack : public Stack<T>
{
private:
	bool empty;
	int count;
	int capacity ;
	T* items;
public:
	Stack();
	Stack(T data);
	Stack(const Stack &s);
	const Stack& operator=(const Stack &s);
	~Stack();
	void push(T data);
	T pop() throw(StackException);
	T top() throw(StackException);
	bool isEmpty();
	void grow();
};

template <typename T>
Stack<T>::Stack()
{
	empty = 1;
	count = 0;
	capacity = START_SIZE;
	items = new T [START_SIZE];
}

template <typename T>
Stack<T>::Stack(T data)
{
	empty = 0;
	count = 1;
	capacity = START_SIZE;
	items = new T [START_SIZE];
	push(data);
}

template <typename T>
Stack<T>::Stack(const Stack<T> & s)
{
	this->empty = s.empty;
	this->count = s.count;
	this->capacity = s.capacity;

	this->items = new T [s.capacity];

	for(int i = 0; i < s.capacity; i++)
	{
		this->items[i] = s.items[i];
	}

}

template <typename T>
Stack<T>::~Stack()
{
	delete [] this->items;
	this->items = NULL;
}

template <typename T>
bool Stack<T>::isEmpty()
{
	return empty;
}

template <typename T>
const Stack<T>& Stack<T>::operator=(const Stack<T> &s)
{
	this->items = new T [s.capacity];
	for(int i = 0; i < s.capacity; i++)
	{
		this->items[i] = s.items[i];
	}
	this->capacity = s.capacity;
	this->count = s.count;
	this->empty = s.empty;
}

template <typename T>
T Stack<T>::pop() throw(StackException)
{
	T index;
	if(empty)
	{
		throw StackException();
	}
	else
	{
		if(count == 1)
		{
			empty = 1;
		}
		
	}

	count--;
	index = items[count - 1];
	items[count - 1] = 0;
	return(index);
}

template <typename T>
void Stack<T>::push (T data)
{
	if (count == capacity)
	{
		grow();
	}
	items[count] = data;
	empty = 0;
	count ++;
}

template <typename T>
T Stack<T>::top() throw(StackException)
{
	return(items[count-1]);
}

template <typename T>
void Stack<T>::grow()
{
	T* temp;
	T *previous;

	previous = items;
	temp = new T[capacity * 2];
	for(int i = 0; i < count; i++)
	{
		temp[i] = previous[i];
	}
	items = temp;
	temp = NULL;
	delete[] previous;
	previous = NULL;
	capacity *= 2;
}


#endif 
Last edited on
mistabob wrote:
9
10
template <typename T>
class Stack : public Stack<T>
Does this make sense? The class extends itself?? Or it extends a class with the same name??

Another thing you should know is that you should define templated classes entirely inline (that is, the function bodies are inside the class definition). Otherwise you'll have some problems that are difficult to fix.
Last edited on
Aha, I think I get it. There's no reason to extend anything, because it's not inheriting member functions from another class?
Yes, if that is how you made sense of what I was saying...

It could still inherit from another class if it wanted, but it does not make sense to inherit from itself.
Last edited on
Works for me. Thanks again.
Topic archived. No new replies allowed.