0

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

steveax
  • 17,527
  • 6
  • 44
  • 59
Nikhil Chilwant
  • 629
  • 3
  • 11
  • 31

5 Answers5

2

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
billz
  • 44,644
  • 9
  • 83
  • 100
1

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

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
0
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)

P0W
  • 46,614
  • 9
  • 72
  • 119
0

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);
}
RiaD
  • 46,822
  • 11
  • 79
  • 123
0

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.

Teeknow
  • 1,015
  • 2
  • 17
  • 39