Samathy
Samathy

Reputation: 57

C++ Passing a pointer (pointing to an object) to function in another class

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.

Upvotes: 0

Views: 9930

Answers (4)

Scott
Scott

Reputation: 1119

There is nothing wrong with the code you have written. There is a typo in your example code, prt_foo = &foo1; should be ptr_foo = &foo1;, but other than that it should work. You don't have to take the pointer to the class foo as a separate line. These lines in the main code will work:

   foo foo1;
   bar bar1;
   bar1.function(&foo1);

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254431

The code you've posted compiles, once the obvious typos are fixed.

Perhaps, in your real code, bar.h doesn't forward declare class foo; so there is no declaration of foo as the compiler says. You'll need to do that, or perhaps include the full definition of foo if the header needs to do anything complicated with it.

Or perhaps foo.h and bar.h both include try to include each other, which is impossible. If that's the case, it should be possible to remove one or both inclusions, replacing them with forward declarations if needed.

If none of this helps, then try to reduce your code to a simple test case which does reproduce the error. If you don't discover the cause of the problem when you do that, then you'll have a good test case to post here.

Upvotes: 0

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

If foo and bar are in separate headers, add forward declaration in bar before the bar class declaration:

class foo;

Upvotes: 2

Chowlett
Chowlett

Reputation: 46667

The error suggests that the file which defines class bar doesn't know about class foo. You need to #include the header file for class foo before your definition for class bar.

Upvotes: 0

Related Questions