I need to pass a pointer which points to an instance of a class into a function which is inside another class so that the function can access some of the public variables of the first class.
Although i am not new to C++ this is the first time i have used pointers in this way and so i don't quite understand how to make this work.
What i have already done is similar to the following:
class foo
{
public:
int data;
};
class bar
{
public:
void function(foo *ptr); //error points to this line
};
void bar::function(foo *ptr)
{
ptr->data = 1; //do something with data inside foo class.
}
main()
{
foo foo1; //init foo as foo1.
foo *ptr_foo; //pointer of type foo called ptr_foo
prt_foo = &foo1; //point ptr_foo to the address of foo1
bar bar1; //init bar as bar1
bar1.function(ptr_foo) //call function1 and pass the address of our instance of foo to it.
}
Im getting errors from class bar saying "syntax error: identifier 'foo'" on the line defining function(foo *ptr).
I'm also getting the error "'bar::function': function does not take 1 arguments" when trying to call function(foo ptr*) in main(), I believe that's linked to the first error yes?
In the program i am working on foo(), bar() and main() are all in different source files linked with header files. does this contribute to my problem?
I'm finding pointers in this context difficult to understand and no amount of reading is helping with this particular problem.
If someone could tell me how to make this work that'd be great, an explanation of whats going on in the solution given solution and why what i have done doesn't work would be even better.
Thanks for any help.
EDIT:Code above is not intended to compile. My actual code is a lot larger than i could reasonably post.
prt_foois not declared;ptr_foois.main, correctprt_footoptr_foo, and add a semi-colon to the last line inmain, it compiles fine.