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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:00:25+00:00 2026-05-31T13:00:25+00:00

I am new to C++ and am not able to correctly define maps in

  • 0

I am new to C++ and am not able to correctly define maps in class definition.

Here is the code I have written:

#include<stdio.h>
#include <iostream>
#include <map>
#include <utility> // make_pair
#include <string.h>
#include <sstream>

using namespace std;

class assgnt
{
private:
    //typedef std::map<int, int> MapType;
    //MapType my_map;
public:
    bool isGoodPoint(int x, int y);
    void addGoodNeighbours(int x, int y);

};

inline bool assgnt::isGoodPoint(int x, int y)
{
    string xs, ys;
    stringstream xout, yout;
    int xsum=0, ysum=0;

    xout<<x; yout<<y;

    xs = xout.str();
    ys = yout.str();

    for each (char c in xs)
    {
        xsum = xsum + int(c);
    }

    for each (char c in ys)
    {
        ysum = ysum + int(c);
    }

    if (xsum+ysum <= 19)
        return true;
    else 
        return false;
}

inline void assgnt::addGoodNeighbours(int x, int y)
{
    //if assgnt::isGoodPoint(x+1,y)
    if(isGoodPoint(x+1,y))
    {

    }
    /*if isGoodPoint(x+1,y) and [x+1,y] not in points: points.append([x+1,y])
    if isGoodPoint(x-1,y) and [x-1,y] not in points: points.append([x-1,y])
    if isGoodPoint(x,y+1) and [x,y+1] not in points: points.append([x,y+1])
    if isGoodPoint(x,y-1) and [x,y-1] not in points: points.append([x,y-1])*/
}


int main()
{

   typedef std::map<int, int> MapType;
    MapType my_map;

    // insert elements using insert function
    my_map.insert(std::pair<int, int>(0, 0));

    int i=0;

    while true
    {


    //my_map.insert(std::pair<int, int>(2, 2));
    //my_map.insert(std::pair<int, int>(3, 3));
    //my_map.insert(MapType::value_type(4, 4)); // all standard containers provide this typedef
    //my_map.insert(std::make_pair(5, 5));      // can also use the utility function make_pair

    MapType::iterator iter = my_map.begin();

    std::cout << "Size of my_map: " << my_map.size() << '\n';

    std::cout << "Enter a key to search for: ";
    int c;
    std::cin >> c;

    iter = my_map.find(c);
    if (iter != my_map.end()) 
    {
        int num = iter->second;
        if(num == 0)
            std::cout << "Value is found: " << iter->second << '\n';
        else 
            std::cout << "Value not found: ";
    }
    else
        std::cout << "Key is not in my_map" << '\n';

//    my_map.clear();
}

I wish to define the map typedef std::map MapType; in the class definition, so that I can use it in all inline functions and the main() by creating its object.

Please help.

  • 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-31T13:00:27+00:00Added an answer on May 31, 2026 at 1:00 pm

    I suppose you want to use it outside of the class? Define it in a public section and be happy. Typedefs are absolutely safe from encapsulation point of view, they are just synonyms. Then you can use it in main as assgnt::MapType.

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

Sidebar

Related Questions

Header file #ifndef IREADER_H #define IREADER_H #include <iostream> class iReader { public: iReader(); iReader(istream
i m not able to modify table in SQL server. i m new to
I am relatively a new Android developer and I am not able to understand
At first, I was not able to build a new blank project to iPhone
In my windows 2003 box, I am not able to create new web applications.
I have a ComboBox, whose ItemsSource is bound to a new (not default) ListCollectionView,
My new phone does not recognize a phone number unless its area code matches
I have done some searching around but have not been able to figure out
I have the following code public abstract class Event { public void fire(Object... args)
I'm not new to Rails or Rspec, but I'm new to making gems. When

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.