Sri Kadimisetty
Sri Kadimisetty

Reputation: 1982

How do i use templated values as keys in a map?

Im trying to implement a 3 dimensional matrix using stl::map.

I have a map whose "keys" are templated and "values" are other maps(for the other dimension). Here is the compiler error I get-

graph.h|37| error: ISO C++ forbids declaration of ‘map’ with no type· 

So is it true that I cannot have a templated value as a map's keys or is there another way to do it. Here is the part of my code where Im attempting to do so-

using namespace std;
template <class V>·
class Graph {
    ...
    map<V, map<V,int> > vertices;·
    ...
};

------ UPDATE:

My comment saying I fixed it is being hidden, the mistake is silly, I should have been using std::map and not map directly. Thanks for helping.

Upvotes: 0

Views: 140

Answers (1)

Alok Save
Alok Save

Reputation: 206526

So is it true that I cannot have a templated value as a map's keys?

No, There is no such rule. If there was any such rule it would mean power of Generic Programming, the very purpose of existence of Templates would be useless.

or is there another way to do it?

You are just having an syntax error, because you did not qualify map with its (std)namespace.

Works fine for me here

#include<map>

template <class V> class Graph 
{
     std::map<V, std::map<V,int> > vertices;

};

int main()
{
    return 0;
}

Upvotes: 3

Related Questions