Accessing member functions of other classes into member function of `this` class using composition or friend classes

StackOverflow https://stackoverflow.com/questions/21544253

  •  06-10-2022
  •  | 
  •  

문제

I am writing a class using 'composition' as follows -

class fibonacci
{
private:
    FibonacciDynamic dy();
    FibonacciRecursive re();
    FibonacciSequential se();
    int count;
public:
    fibonacci(int a):count(a){};
    void disp();
};

void fibonacci::disp()
{
    if(count < 20)
    {
        se.fib();
    }
    else if(count < 50)
    {
        re.fib();
    }
    else
    {
        dy.display();
    }
}

Here, FibonacciDynamic, FibonacciRecursive & FibonacciSequential are classes declared in header files. Now, the main problem here is that while using se.fib(), re.fib & dy.fib() functions it gives me error like

error C2228: left of '.fib' must have class/struct/union

Is there any other way to use composition approach here without getting above errors?

If not then is it possible to use them as friend classes & access their member functions in a member function of fibonacci class?

Thanks.

도움이 되었습니까?

해결책

FibonacciDynamic dy(); is declaration of method FibonacciDynamic fibonacci::dy();

you should remove parentheses to make it data member declaration:

...
FibonacciDynamic dy;
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top