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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:53:37+00:00 2026-06-02T08:53:37+00:00

I am having trouble accessing data inside a map using an iterator. I want

  • 0

I am having trouble accessing data inside a map using an iterator. I want to return all of the values inserted into the map by using an iterator. However, when I use the iterator, it doesn’t acknowledge any of the members in the instance of the class it has been past into.

int main()
{
    ifstream inputFile;
    int numberOfVertices;
    char filename[30];
    string tmp;

    //the class the holds the graph
    map<string, MapVertex*> mapGraph;

    //input the filename
    cout << "Input the filename of the graph: ";
    cin >> filename;
    inputFile.open(filename);

    if (inputFile.good())
    {
        inputFile >> numberOfVertices;
        inputFile.ignore();

        for (int count = 0; count < numberOfVertices; count++)
        {
            getline(inputFile, tmp);
            cout << "COUNT: " << count << "  VALUE: " << tmp << endl;

            MapVertex tmpVert;
            tmpVert.setText(tmp);
            mapGraph[tmp]=&tmpVert;
        }

        string a;
        string connectTo[2];

        while (!inputFile.eof())
        {

            //connectTo[0] and connectTo[1] are two strings that are behaving as keys

            MapVertex* pointTo;
            pointTo = mapGraph[connectTo[0]];
            pointTo->addNeighbor(mapGraph[connectTo[1]]);
            //map.find(connectTo[0]).addNeighbor(map.find(connectTo[1]));
            //cout << connectTo[0] << "," << connectTo[1] << endl;
        }

        map<string,MapVertex*>::iterator it;
        for (it=mapGraph.begin(); it!=mapGraph.end(); it++)
        {
            cout << it->getText() << endl;

        }
    }

    return 0;
}

Compiler output:

\lab12\main.cpp||In function `int main()':|
\lab12\main.cpp|69|error: 'struct std::pair<const std::string, MapVertex*>'
                           has no member named 'getText'|
||=== Build finished: 1 errors, 0 warnings ===|

There is an access member in my MapVertex class called getText() which returns the data that is inside it.

  • 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-02T08:53:42+00:00Added an answer on June 2, 2026 at 8:53 am

    To fix the compiler error, you need to do it->second->getText() as the *iterator is a pair<string, MapVertex*>. But there are other problems in your code. While inserting into the map, you are inserting the address of a local variable into it. This address will be invalid by the time you try to iterate the map using for loop. I would suggest you to declare the map as std::map<string, MyVertex> so that when you insert into the map a copy of the MyVertex is inserted into map.

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

Sidebar

Related Questions

I am having trouble accessing the data field. I receive the error: Databinding methods
CakePHP Newbie :) I am having trouble accessing another controller and passing that data
I'm having trouble accessing the id , area and theme values in my ViewData.
I'm having trouble creating an Access table using VBA, then accessing it via open-recordset.
I am having trouble accessing the value of an HTML Input element using jQuery,
I'm having trouble accessing a dataset. I've pulled data from an Access database (successfully
For some reason I'm having trouble accessing the fopen() function from inside a class
I'm using Silo with HDF5, and I'm having trouble accessing some of the metadata
I've seen; Accessing Google Spreadsheets with C# using Google Data API and http://code.google.com/apis/spreadsheets/data/2.0/developers_guide_dotnet.html#CreatingRows However
I'm having trouble accessing the review_history using the getInfoFor method, example code below :

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.