Getting and error but I don't know why.

Here is my slist.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
#ifndef SLIST_H
#define SLIST_H

#include"snode.h"
#include"slistiterator.h"
#include<stdexcept>
using std::out_of_range;

template<typename generic>
class SList
{
 public:
  SList();
  SList(const SList& s);
  ~SList();
  void push_front(generic x);
  void pop_front();
  void remove(generic x);
  generic& front();
  const generic& front() const;
  void clear();
  unsigned int size() const;
  bool empty() const;
  typedef SListIterator<generic> Iterator;
  Iterator begin() const;
  Iterator end() const;
  SList& operator=(const SList& s);

 private:
  unsigned int m_size;
  SNode<generic>* m_front;
};


#include"slist.hpp"
#endif 


In my slist.hpp file it gives me the errors on these two functions.

1
2
3
4
5
6
7
8
9
10
11
template<typename generic>
Iterator SList<generic>::begin() const // this line
{
	return Iterator(m_front);
}

template<typename generic>
Iterator SList<generic>::end() const // this line
{
	return Iterator();
}

The errors are:

slist.hpp:135: error: expected constructor, destructor, or type conversion before âSListâ
slist.hpp:141: error: expected constructor, destructor, or type conversion before âSListâ

I don't understand why it is saying this because I have the
typedef SListIterator<generic> Iterator;
So shouldn't Iterator be the type conversion before SList?
If I didn't give enough information to solve the problem or said something that doesn't make any sense please let me know, I am fairly new to all of this.
Thanks for all the help. :)
Last edited on
Please edit your post and put the source inside code tags. It will make your post a lot more legible and folks here will be more likely to look at it.
The problem is that the compiler does not know which Iterator you are referring in the return type of begin() and end()

Try changing the return type from Iterator to SList<generic>::Iterator in the definitions of begin() and end().
Last edited on
If you mean like this:

1
2
3
4
5
6
7
8
9
10
11
template<typename generic>
SList<generic>::Iterator SList<generic>::begin() const
{
	return Iterator(m_front);
}

template<typename generic>
SList<generic>::Iterator SList<generic>::end() const
{
	return Iterator();
}


It doesn't work it gives me the same error as before.
You need the typename keyword qualifier as well

1
2
3
4
5
6
7
8
9
10
11
template<typename generic>
typename SList<generic>::Iterator SList<generic>::begin() const
{
    return Iterator(m_front);
}

template<typename generic>
typename SList<generic>::Iterator SList<generic>::end() const
{
    return Iterator();
}
Ok it works. Thanks for all the help.
Topic archived. No new replies allowed.