B Seven
B Seven

Reputation: 45943

How do you create a hash table in C++?

I am creating a simple hash table in VS 2008 C++.

#include <map>
std::map <string, char> grade_list;
grade_list["John"] = 'B';

I am getting the error: error C2057: expected constant expression

What does that mean? Does boost library have something better?

Thanks!

Upvotes: 0

Views: 2241

Answers (3)

B Seven
B Seven

Reputation: 45943

The code was before main function.

Upvotes: 1

Aif
Aif

Reputation: 11220

#include <map>
#include <iostream>
#include <string>

int main() {
    std::map<std::string, char> grade_list;
    grade_list["John"] = 'B';
    std::cout << grade_list["John"] << std::endl;
    return 0;
}

This works great with g++. You should specify std:: before string in your map declaration, as I did in my code.

Upvotes: 6

sepp2k
sepp2k

Reputation: 370112

First of all std::map is a treemap, not a hashmap.

The reason you get the error is that you did not #include <string> nor qualify the reference to string and thus the compiler does not know that string is a class.

Upvotes: 10

Related Questions