Tuesday, 10 September 2013

C++: Passing classes as argument to methods of other classes

C++: Passing classes as argument to methods of other classes

I want to pass a class pointer to a function which is a member of another
class and it won't work. If the function fill_packet is not a member of
any class, the code compiles without problems:
// WORKS FINE:
class Packet{
private:
char *contents;
public:
void pack();
};
void fill_packet(Packet *a){
a->pack();
};
However, if the function fill_packet is inside another class (here
Worker), it suddenly does no longer compile as soon as I try to access a
method of a.
// WON'T COMPILE:
// error: 'Packet' has not been declared
class Packet{
private:
char *contents;
public:
void pack();
};
class Worker{
public:
void fill_packet(Packet *a){
a->pack();
};
};
Can someone give me insight? thanks in advance

No comments:

Post a Comment