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

  • SEARCH
  • Home
  • 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 8146207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:55:06+00:00 2026-06-06T13:55:06+00:00

Consider a std::map<const char *, MyClass*> . How do I access a member (variable

  • 0

Consider a std::map<const char *, MyClass*>.

How do I access a member (variable or function) of the MyClass object pointed to by the map?

// assume MyClass has a string var 'fred' and a method 'ethel'
std::map<const char*, MyClass*> MyMap;

MyMap[ "A" ] = new MyClass;
MyMap.find( "A" )->fred = "I'm a Mertz";  // <--- fails on compile
MyMap.find( "A" )->second->fred = "I'm a Mertz";  // <--- also fails

EDIT — per Xeo’s suggestion

I posted dummy code. Here is the real code.

// VarInfo is meta-data describing various variables, type, case, etc.
std::map<std::string,VarInfo*> g_VarMap; // this is a global 

int main( void )
{ 
   // ........ g_VarMap["systemName"] = new VarInfo; 
   g_VarMap.find( "systemName" ).second->setCase( VarInfo::MIXED, VarInfo::IGNORE ); 
   // ..... 
} 

errors were:

struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VarInfo*> >’ has no member named ‘second’
Field 'second' could not be resolved Semantic Error make: *** [src/ACT_iod.o] Error 1 C/C++ Problem
Method 'setCase' could not be resolved Semantic Error – 
  • 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-06T13:55:09+00:00Added an answer on June 6, 2026 at 1:55 pm

    std::map stores types internally as a std::pair, and std::map::find, returns an iterator. So, to access members of your class, you have to go through the iterator, which presents the key_type as first, and the value_type as second. Also, as others have stated, you should probably not be using const char* as your key_type. Here’s a short example.

    #include <string>
    #include <map>
    #include <iostream>
    
    struct T
    {
       T(int x, int y) : x_(x), y_(y)
       {}
    
       int x_, y_;
    };
    
    int main()
    {
       typedef std::map<std::string, T> map_type;
       map_type m;
    
       m.insert(std::make_pair("0:0", T(0,0)));
       m.insert(std::make_pair("0:1", T(0,1)));
       m.insert(std::make_pair("1:1", T(1,1)));
    
       // find the desired item (returns an iterator to the item
       // or end() if the item doesn't exist.
       map_type::const_iterator t_0_1 = m.find("0:1");
    
       if(m.end() != t_0_1)
       {
          // access via the iterator (a std::pair) with 
          // key stored in first, and your contained type
          // stored in second.
          std::cout << t_0_1->second.x_ << ':' << t_0_1->second.y_ << '\n';
       }
    
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this situation: void doSmth1(std::map<int,int> const& m); void doSmth2(std::map<int,int> const& m) { std::map<int,int> m2
For a specific example, consider atoi(const std::string &) . This is very frustrating, since
consider this translation unit: #include <map> #include <string> int main() { std::map<std::string, std::size_t> mp;
Consider the following program: #include <cstddef> #include <cstdio> void f(char const*&&) { std::puts(char const*&&);
Consider the following function prototypes: void Remove(SomeContainer& Vec, const std::size_t Index); SomeContainer Remove(SomeContainer Vec,
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
Consider this line: std::wcout << Hello World!; Is it OK to pass char* or
Consider the following code: std::string my_error_string = Some error message; // ... throw std::runtime_error(std::string(Error:
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Consider: delete new std :: string [2]; delete [] new std :: string; Everyone

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.