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 8915095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:53:42+00:00 2026-06-15T04:53:42+00:00

Why does my map inside Test does not return its value? What’s wrong with

  • 0

Why does my map inside Test does not return its value? What’s wrong with this?

class Test{ //Test.h
public:
  std::map< char*,  int> mm;

  Test();
  void set();
  int get( char*);
};

Test::Test(){ //Test.cpp
}

void Test::set(){
  mm["aaa"] = 24;
}

int Test::get( char* n){
  return mm[n];
}

int main(){ //main.cpp
   Test *test = new Test();
   test->set();

   //this returns 0 instead of 24
   printf("From Test: %d\n", test->get("aaa"));
   printf("From Test: %d\n", test->mm["aaa"]);

   delete test;

   //this map works
   std::map<char*, int> mm;
   mm["a"] = 54;

   printf("Local: %d\n", mm["a"]);

   return 0;
}

Needs extra text to post >.<

Oops! Your question couldn’t be submitted because:

Your post does not have much context to explain the code sections; please explain your scenario more clearly.

  • 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-06-15T04:53:43+00:00Added an answer on June 15, 2026 at 4:53 am

    It fails because you use char* pointer as a key. For two literals addresses will be different. To make map work properly you must use some other string class which has operator< defined. std::string, for instance.

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

Sidebar

Related Questions

Am I right in assuming that adding/removing elements to an std::map does not effect
Does map() iterate through the list like for would? Is there a value in
Does std::map move around already inserted values when inserting new data ?
When we create an object of a class what does it memory map look
If I use map or filter inside another function, does that function become a
Why does trying to compile public class GenericsFail { public static void main(String[] args)
Im getting this Error when loading an ESRI map inside the Android Environment. This
I have this class and these methods inside of it. class Purchase < ActiveRecord::Base
Below is the class used as the value in a map: class Book {
Looking at this get value of inside a tag with jQuery.? <span> <b>hi_1</b> <b>hi_2</b>

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.