My return value from an iterator over a set gives the memory location instead of the value. How do I access the element the iterator is pointing to?
I used a similar iterator loop before which worked fine, as in returning the value the iterator is pointing to (for both a deque and vector template class).
This question is a follow up on a script I had difficulties with earlier today (C++ : adding an object to a set).
The script now looks as follows:
header file
#ifndef EMPLOYEE_HH
#define EMPLOYEE_HH
#include <set>
#include <string>
#include <iostream>
using namespace std ;
class Employee {
public:
// Constructor
Employee(const char* name, double salary) :
_name(name),
_salary(salary) {
}
// Accessors
const char* name() const {
return _name.c_str() ;
}
double salary() const {
return _salary ;
}
// Print functions
void businessCard(ostream& os = cout) const {
os << " +--------------------+ " << endl
<< " | ACME Corporation | " << endl
<< " +--------------------+ " << endl
<< " Name: " << name() << endl
<< " Salary: " << salary() << endl ;
}
private:
string _name ;
double _salary ;
} ;
class Manager : public Employee {
public:
//Constructor
Manager(const char* _name, double _salary):
Employee(_name, _salary),
_subordinates() {
}
// Accessors & modifiers
void addSubordinate(Employee& empl) {
_subordinates.insert(&empl);
}
const set<Employee*>& listOfSubordinates() const {
return _subordinates;
}
void businessCard(ostream& os = cout) const {
Employee::businessCard() ;
os << " Function: Manager" << endl ;
set<Employee*>::iterator iter ;
iter = _subordinates.begin() ;
os << " Subordinates:" << endl ;
if(_subordinates.empty()==true) {
os << " Nobody" << endl;
}
while(iter!=_subordinates.end()) {
os << " " << *iter << endl ; // <-- returns the memory location
++iter ;
}
}
private:
set<Employee*> _subordinates ;
} ;
#endif
Main script
#include <string>
#include <iostream>
#include "Employee.hh"
using namespace std ;
int main() {
Employee emp1("David", 10000) ;
Employee emp2("Ivo", 9000) ;
Manager mgr1("Oscar", 18000) ; // Manager of Ivo and David
Manager mgr2("Jo", 14000) ;
Manager mgr3("Frank", 22000) ; // Manager of Jo and Oscar (and Ivo and David)
mgr1.addSubordinate(emp1) ;
mgr1.addSubordinate(emp2) ;
mgr3.addSubordinate(mgr1) ;
mgr3.addSubordinate(mgr2) ;
cout << '\n' ;
emp1.businessCard() ;
cout << '\n' ;
emp2.businessCard() ;
cout << '\n' ;
mgr1.businessCard() ;
cout << '\n' ;
mgr2.businessCard() ;
cout << '\n' ;
mgr3.businessCard() ;
cout << '\n' ;
return 0;
}
Any help is much appreciated.
If this: *iter
is an address then this: *(*iter)
is the value that *iter
is pointing to.
This should work in your case:
while(iter != _subordinates.end())
{
os << " " << **iter << endl ; // <-- returns the value which is the set
++iter;
}
Edit: This fixed the issue: (*iter)->name()
That would be **iter
; one dereference for the iterator, one for the pointer the iterator refers to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With