Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1055573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:35:00+00:00 2026-05-16T17:35:00+00:00

This is some sample code copied from The C++ Programming Language chapter 17 as

  • 0

This is some sample code copied from The C++ Programming Language chapter 17 as below. When I compile it with Visual Studio 2008, it keeps giving me this error:
warning C4346: 'HashMap<Key,T,H,EQ,A>::mapped_type' : dependent name is not a type
1> prefix with 'typename' to indicate a type

Does anyone have any idea about this?
Thanks in advance!

#include <vector>
#include <map>
using std::vector;
using std::pair;
using std::iterator;

template<class Key, class T, class H= Hash<Key>, class EQ = equal_to<Key>, class A = allocator<pair<const Key, T>>>
class HashMap{
public:
    typedef Key key_type;
    typedef T mapped_type;
    typedef pair<const Key, T> value_type;
    typedef typename A::size_type size_type;
    typedef H Hasher;
    typedef EQ key_equal;

    HashMap(const T& dv=T(), size_type n = 101, const H& hf = H(), const EQ& = EQ()):
            :default_value(dv), b(n), no_of_erased(0), hash(hf), eq(e){
        set_load();
        v.reserve(max_load*b.size());
    }
    template<class In> HashMap(In first, In last,
        const T& dv=T(), size_type n=101, const H& hf=H(), const EQ& = EQ());

    void set_load(float m=0.7, float g=1.6){
        max_load = m;
        grow = g;
    }
    mapped_type& operator[](const key_type& k);

    void resize(size_type n);
    void erase(iterator position);
    size_type size() const { return v.size() - no_of_erased;}
    size_type bucket_count() const { return b.size();}
    Hasher hash_fun() const { return hash;}
    key_equal key_eq() const { return eq;}

private:
    struct Entry{
        key_type key;
        mapped_type val;
        bool erased;
        Entry* next;
        Entry(key_type k, mapped_type v, Entry* n):
            key(k),val(v),erased(false),next(n) {}
    };

    vector<Entry> v;
    vector<Entry*> b;

    float max_load;
    float grow;
    size_type no_of_erased;
    Hasher hash;
    key_equal eq;

    const T default_value;

};


template<class Key, class T, class H=Hash<Key>, class EQ=equal_to<Key>, class A=allocator<pair<const Key,T>>> 
HashMap<Key,T,H,EQ,A>::mapped_type & HashMap<Key,T,H,EQ,A>::operator [](const HashMap<Key,T,H,EQ,A>::key_type& k){
    size_type i = hash(k)%b.size();

    for(Entry* p=b[i]; p; p=p->next){
        if(eq(k, p->key)){
            if(p->erased){
                p->erased = false;
                no_of_erased--;
                return p->val=default_value;
            }
            return p->val;
        }
    }

    if(size_type(b.size()*max_load) <= v.size()){
        resize(b.size()*grow);
        return operator[](k);
    }

    v.push_back(Entry(k,default_value,b[i]));
    b[i] = &v.back();

    return b[i]->val;
}

template<class Key, class T, class H=Hash<Key>, class EQ=equal_to<Key>, class A=allocator<pair<const Key,T>>> 
void HashMap<Key,T,H,EQ,A>::resize(size_type s){
    size_type i = v.size();
    while(no_of_erased){
        if(v[--i].erased){
            v.erase(&v[i]);
            --no_of_erased;
        }
    }

    if(s<=b.size())
        return;
    b.resize(s);
    fill(b.begin(),b.end(),0);
    v.reserve(s*max_load);

    for(size_type i=0;i<v.size(); i++){
        size_type ii = hash(v[i].key)%b.size();
        v[i].next = b[ii];
        b[ii] = &v[i];
    }
}

template<class Key, class T, class H=Hash<Key>, class EQ=equal_to<Key>, class A=allocator<pair<const Key,T>>>
void HashMap<Key,T,H,EQ,A>::erase(iterator p){
        if(p->erased == false) no_of_erased++;
        p->erased = true;
}


int main(){
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:35:01+00:00Added an answer on May 16, 2026 at 5:35 pm

    Change

    HashMap<Key,T,H,EQ,A>::mapped_type & HashMap<Key,T,H,EQ,A>::operator []
    

    to

    typename HashMap<Key,T,H,EQ,A>::mapped_type & HashMap<Key,T,H,EQ,A>::operator []
    

    as your error already suggests. The compiler cannot deduce on it’s own that mapped_type is a typedef inside a class template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use some sample code and my compiler won't compile this
Background We've been using some code copied verbatim from Joe Duffy's Concurrent Programming on
Can someone write some sample code to explain this concept? I know what a
I'm teaching myself Python and I was translating some sample code into this class
hope someone can answer this. Here is some sample code. namespace std { #ifdef
This is some simple code that draws to the screen. GLuint vbo; glGenBuffers(1, &vbo);
Why doesn't object.__init__ take *args, **kwargs as arguments? This breaks some simple code in
Thanks to this site and a few others, I've created some simple code to
I don't know how to explain this correctly but just some sample for you
<p title=The test paragraph>This is a sample of some <b>HTML you might<br>have</b> in your

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.