Function datatype return issue

I have problem understanding how to declare functions to return needed data type. I only placed part of code that I have problems with.
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
template <class Data>
class Set{
public:
  typedef  ?????? const_iterator;
  typedef  ?????? iterator;
.
.
.
const_iterator end() const;
.
.
.
private:
  unsigned int numOfData;
  std::list<Data> data;
};
.
.
.
template <class Data> 
inline
typename Set<Data>::const_iterator
  Set<Data>::end () const
{
    return data.end();
}


I'm trying to set Set::const_iterator to return list::iterator (return data.end();). To do this, what do I need to declare Set const_iterator and iterator (marked with ????) to make it work as I intended?

Last edited on
I think you want a template function pointer. In which case the data type for "const_iterator" would be "Data", as in the stand in name of your template. You also need to rewrite Line 9 so that "end()" is a template as well.
Could you please be more specific. I dont think I get it.
Last edited on
Line 4:
typedef Data const_iterator;

Line 9 should actually be fine now that I see it again.
I have tried that as well as
1
2
typedef std::list<Data> const_iterator;
typedef const std::list<Data> const_iterator;


Anything I placed so far would give me a similar error at -> return data.end();

error: conversion from 'std::_List_const_iterator<int>' to non-scalar type 'const std::list<int, std::allocator<int> >' requested|

Return statement returns list datatype so const_iterator should be declared as that data type I guess, but so far I had not have luck with it..
Last edited on
One more thing to say it that this code works using pointers to arrays but to convert it to use std::list I get stuck on this part I had previously posted. I have not be able to figure this one yet, any hints would help..



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
template <class Data>
class Set{
public:
  typedef  const Data* const_iterator;
  typedef  const Data* iterator;
.
.
.
const_iterator end() const;
.
.
.
private:
  unsigned int numOfData;
 Data *data;
};
.
.
.
template <class Data> 
inline
typename Set<Data>::const_iterator
  Set<Data>::end () const
{
    return data + numOfData;
}
Last edited on
I'm trying to set Set::const_iterator to return list::iterator
1
2
typedef typename std::list<Data>::iterator iterator;
typedef typename std::list<Data>::const_iterator const_iterator;
That's it :)

Never thought that it is appropriate in c++ to declare things this way having typedef and typename next to each other. That was what was confusing me.


Thank you ne555 and computergeek01 for your help.
The typename isn't needed, it will just help you find bugs by giving an informative compile error if you'd used a container besides std::list that had a data member named iterator instead of a type.

EDIT: Sorry, I'm still figuring out this stuff myself. See ne555's post below.
Last edited on
Topic archived. No new replies allowed.