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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:25:37+00:00 2026-05-26T19:25:37+00:00

I have a set of pointers to objects of the following class: class name_id

  • 0

I have a set of pointers to objects of the following class:

class name_id {
    public:
        name_id(string name_a, int id_a) { name = name_a; id = id_a; }
        string name;
        int id;
        bool operator<(name_id &);
};

Later, after populating the set, I pass this set to a function and try to access the name field of each object:

void print_by_rank(set<name_id *> &nameIdSet, map<int, int> &votesMap)
{
    vector<string> names;
    vector<string>::iterator names_it;

    set<name_id *>::iterator nameIdSet_it;
    for(nameIdSet_it = nameIdSet.begin(); nameIdSet_it != nameIdSet.end(); ++nameIdSet_it)
        //names.push_back(nameIdSet_it->name);
        cout << nameIdSet_it->name << endl;
}

However, neither of the bottom two lines will compile. I receive the following message:

g++ -o bobwinner bobwinner.cpp
bobwinner.cpp: In function 'void print_by_rank(std::set<name_id*, std::less<name_id*>, std::allocator<name_id*> >&, std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >&)':
bobwinner.cpp:68: error: request for member 'name' in '* nameIdSet_it.std::_Rb_tree_const_iterator<_Tp>::operator-> [with _Tp = name_id*]()', which is of non-class type 'name_id* const'

I do not understand why I cannot use the arrow operator on an iterator to access the name field of the object to which the set element points.

  • 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-26T19:25:38+00:00Added an answer on May 26, 2026 at 7:25 pm

    You need to dereference the iterator to get the pointer. You then need to dereference the pointer to get at the object itself. So you probably want:

    cout << (*nameIdSet_it)->name << endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an stl set with objects of Cell class class Cell { public:
I am using EF with the following definitions: public class StoredFile { public int
I have a program doing something like the following: class SomeObject{} { void function(int
I have the following code: int start = [html rangeOfString:@class=WordSection1>].location + 24; int end
I have the following code class sample { public void update(List l) { l
I have a class, and the pointers to the objects of this class need
I have a set of bitsets pointers in an unordered_map static unordered_map< size_t, bitset<BITSIZE>*
I have a set of shared pointers: std::set<boost::shared_ptr<T>> set; And a pointer: T* p;
If I have set<Pointer*> , Pointer* is pointer to custom class, set is from
hi have a COM visible API in C# which looks like the following: public

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.