I'm having a problem with inheriting from STL set (i think):
Here is class Prime:
class Prime : public set<A> {
private:
// private data members.
public:
// C'tor...
void printParticularA(const int& id);
}
Here is class A:
class A : public List<B>{
private:
// data members.
int id;
public:
// C'tor
A(const A& copy) : List<B>(copy), //copy data members
{ // validate data and throw exceptions if needed. };
bool operator< (const A& rhs) const{
return id < rhs.id;
}
void printReport() const {
for(const B& item : *this){ item.print(); }
}
}
now here is the problem. in the next function i want to print a particular A object in the set:
void Prime::printParticularA(const int& id) {
find(AFinder(id))->printReport();
}
i also tried this:
void Prime::printParticularA(const int& id) {
*(find(AFinder(id))).printReport();
}
note: assume that class B has print() method.
note2: AFinder is a class for making dummy A objects using only the id data.
the problem is that when 'find' finds the objects it returns const_iterator (because every object in set is const), and when i dereference it i get a copy of the object (??) but the list of B inside it is empty!
this happens also for the '->' version.
now i know that set doesn't allow me to change the objects but i do not intend to change the object (as you can see in the declaration of printReport member function).
i appreciate any help in this!
EDIT: thanks everyone, you have helped me a lot especially learning what not to do.
I solved the problem and it wasn't in set, list nor any of my classes presented here.
my mistake was in understanding the question i was given (yes this is my homework assignment, i'm still new to c++).
sorry if you feel i have wasted your time.
i hope that i can learn from all of your experience and someday help others!
in short, THANKS!! :)