tags:

views:

83

answers:

3

How i return iterator form function :

i worte this : . ..

template<class S,class T> class Database {
public:
.
..
 map<S,Node<T>*> m_map::iterator Find (S keyToFind);
.
..
....


private:
.
..
 map<S,Node<T>*> m_map;
..
.
};

.
..
template<class S,class T>
map<S,Node<T>*> m_map::iterator Find (S keyToFind) {
 map<S,Node<T>*>::iterator itMap;
 itMap = m_map.find(KeyToUpDate);
 return itMap;
}
..
.

there are many error because this : Error 1 error C2653: 'm_map' : is not a class or namespace name Error 2 error C2146: syntax error : missing ';' before identifier 'Find' Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default- Error 5 error C2653: 'm_map' : is not a class or namespace name Error 7 error C2133: 'iterator' : unknown size .. ...

i don't understand what is the problem..

+3  A: 

Your Find function should be defined as:

template<class S,class T>
typename map<S,Node<T>*>::iterator Find (S keyToFind) {
    map<S,Node<T>*>::iterator itMap;
    itMap = m_map.find(KeyToUpDate);
    return itMap;
}

without the " m_map" that you had as part of the function's return type.

Edit: Though actually, there's no need to create the temporary itMap iterator; you can return the result of find directly. Also, I think that KeyToUpDate should instead be keyToFind. Making those modifications, you'd end up with:

template<class S,class T>
typename map<S,Node<T>*>::iterator Find (S keyToFind) {
    return m_map.find(keyToFind);
}
Josh Townzen
+2  A: 

This should be:

template<class S,class T>
typename map<S,Node<T>*>::iterator Find(S keyToFind) {
    typename map<S,Node<T>*>::iterator itMap;
    itMap = m_map.find(KeyToUpDate);
    return itMap;
}

and

typename map<S,Node<T>*>::iterator Find (S keyToFind);

typename is needed because iterator is a dependent type, see e.g. here.

Georg Fritzsche
+6  A: 

Looks like you want:

typename map<S,Node<T>*>::iterator

You should really use typedef's to clean this stuff up:

template<class S, class T>
class Database
{
public:
    // I hope you have't put `using namespace std;` in a header...
    typedef std::map<S, Node<T>*> map_type;
    typedef typename map_type::iterator iterator;
    // etc...

    // use references to avoid copying
    iterator Find (const S& keyToFind);
private:
    map_type m_map;
};
GMan
First comment should be "I hope you have NOT put 'using namespace std' in a header." I hope.
Zan Lynx
@Zan: Ha, thanks. :)
GMan