Nikhil Chilwant
Nikhil Chilwant

Reputation: 649

How to use instance of one class in a method of another class?

I defined two classes class A and class B. They are completely independent.

Now I create c as instance of class B and d as instance of class A. Now I want to define the body of functionOfA that will perform operations on c:

class A {
public:
    functionOfA();
}

class B {
    ...
}

A::functionOFA()
{
    c.functionOfB();
}

main()
{
    A d;
    B c;
    d.functionOfA();
}

but the compiler gives me the following error: c is not declared in this scope

Upvotes: 0

Views: 160

Answers (5)

Teeknow
Teeknow

Reputation: 1015

The reason you're getting this error is that c is not accessible by any object of class A. To change this you can do either of two things: you can either give A a datamember of type B and use that or pass in an object of type B to function of A.

class A {
public:
    B objB;
    functionOfA();
}

class B {
    ...
}

A::functionOFA()
{
    objB.functionOfB();
}

main()
{
    A d;
    B c;
    d.objB = c;
    d.functionOfA();
}

OR (keeping them independent)

class A {
public:
    functionOfA(B& objB);
}

class B {
    ...
}

A::functionOFA(B& objB)
{
    objB.functionOfB();
}

main()
{
    A d;
    B c;
    d.functionOfA(c);
}

Check out dependency injection. I think you can make A sort of independent from B's type in the first scenario with that technique.

Upvotes: 0

LihO
LihO

Reputation: 42083

In this code (your main):

{
    A a;
    B b;
    a.functionOfA();
}

b is a local variable usable only within this scope. b represents an object with automatic storage duration that exists until the execution goes out of the scope (in this case: out of your main).

When you call the method functionOfA, although the object b still "lives", the functionOfA has no means of accessing this object ~> this method needs a reference to this object (or its copy) to use it:

class A {
public:
    void functionOfA(B& b) {
        /* b is now accessible here, this method can also change the object b */
    }

called in this manner:

A a;
B b;
a.functionOfA(b);

I recommend you to also have a look at these questions:
How to pass objects to functions in C++?
Is it better in C++ to pass by value or pass by constant reference?
Are there benefits of passing by pointer over passing by reference in C++?

and some good book might be very helpful here too:
The Definitive C++ Book Guide and List

Upvotes: 1

RiaD
RiaD

Reputation: 47619

I don't understand how they are completely independent if 1 call method of another one. But You can pass c as argument.

class B
{public:
    functionOfB();
}

class A
{public:
    functionOfA(B /*or B& or const B&, learn about references*/ c);
    .....;
}



A::functionOFA(B c)
{
    c.functionOfB();
}

B::functionOfB()
{
  .....
}

main()
{
    A d;
    B c;
    d.functionOfA(c);
}

Upvotes: 0

billz
billz

Reputation: 45410

A::functionOfA() definition needs to know what's c instance, you could pass in B instance:

class B;

class A
{
public:
    functionOfA(const B& b) const;
};

A::functionOFA(const B& b) const
{
    b.functionOfB();
}

A a;
B b;
a.functionOfA(b); // pass in B instance

Upvotes: 2

P0W
P0W

Reputation: 47784

A::functionOFA()
{
    c.functionOfB();  //You cannot do this unless c is global
}

However in your case its in main

Use:

 B c;
void A::functionOfA()
{
    c.functionOfB();
}

OR

pass Object of B as an argument functionOfA(const B& c)

Upvotes: 0

Related Questions