Hi all, i'm new to Templates and when i tried to implement templates methods which needs to use a map (which is a member in private), the map can only do 3 things: insert, swap, and operator =. and i don't get the full functionality of map..

Here's the code:

#ifndef __GARAGE_H_
#define __GARAGE_H_
#include <map>
using namespace std;
template <class T,class SortKey, class SearchKey>
class GarageDataBase
{
public :

	GarageDataBase();
	virtual ~GarageDataBase();	
    const T& Top() const;
	bool Add(T data,SortKey key2, SearchKey key2);
	T Remove(SearchKey toRemove);
	T Find(SearchKey toFind) const;
	bool isEmpty()const;
	


private:
	multimap<SortKey,T> firstMap;
	multimap<SearchKey,pair<SortKey,T>*> secondMap;

};
#endif



template <class T,class SortKey, class SearchKey> GarageDataBase<T,SortKey,SearchKey>::GarageDataBase()
{

}

template <class T,class SortKey, class SearchKey> GarageDataBase<T,SortKey,SearchKey>::~GarageDataBase()
{
}

template <class T,class SortKey, class SearchKey> const T& GarageDataBase<T,SortKey,SearchKey>::Top() const
{
	firstMap.
}

in the last func when trying to get into firstMap mathods, all i get is: insert, swap or, =...


how do i get to "first" or s"econd" in the map?


Thank you and sorry i'm just a beginner here

Dani AI

Generated

Short answer: use iterators and the iterator's ->first / ->second. IntelliSense can be noisy inside templates (as noted), but the compiler and correct code will work. As suggested, obtain an iterator to the element and dereference it; because your Top() is const use a const_iterator (or cbegin() / auto in modern C++), and always check for empty before dereferencing.

Example (pre-C++11 style — note the required typename for a dependent nested type):

const T& GarageDataBase<T,SortKey,SearchKey>::Top() const {
    if (firstMap.empty()) throw std::out_of_range("Top on empty");
    typename std::multimap<SortKey,T>::const_iterator it = firstMap.begin();
    return it->second;   // iterator->first is the key, ->second is the stored value
}

Modern C++ (C++11+) can be simpler with auto / cbegin():

const T& Top() const {
    if (firstMap.empty()) throw std::out_of_range("Top on empty");
    auto it = firstMap.cbegin();
    return it->second;
}

Notes and quick tips:

  • Use rbegin()/crbegin() if you want the largest key instead of the smallest.
  • Returning a reference to the mapped value is fine so long as the element remains in the map (erasing it later invalidates the reference).
  • Avoid using namespace std; in headers.
  • For your secondMap, prefer storing an iterator into firstMap (or a typedef/using alias for the iterator type) rather than a raw pointer to a pair; iterators are the canonical way to refer back to map elements.

See cppreference for the typename rule and multimap behavior: and std::multimap.

Recommended Answers

All 8 Replies

Try using iterators.

template <class T,class SortKey, class SearchKey> const T& GarageDataBase<T,SortKey,SearchKey>::Top() const
{
	mulimap<SortKey,T>::iterator it;
	it=firstMap.
}

still doesn't let me to firstMap.begin()

i want to return the first element in the map

What do you mean "doesn't let me"? What happens if you change line 5 above to say

it=firstMap.begin();

?

it doesn't complete me with Intellisense code completion..

Maybe its the completion system problem?

I would not expect code completion to work flawlessly in the presence of templates, because in general it's not possible to know for certain what types are involved. What happens if you enter by hand the statement I suggested and compile it?

it seems to complie however i still doesn't understand why can't i accsses STL map methods?

You can -- you just did.

The trouble is that in general, when the type of an object includes a template parameter, it is not always possible to determine what all of its members are.

Ok, Thanks for the help!

I'll try using the methods created and update if it actually works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.