CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    Member function access

    Hi,

    I have two classes : ClassA and ClassB. Now I want a member function
    in ClassA to call a member function in ClassB. I tried to make ClassA
    a friend of ClassB but compiler compains.

    Is there any way around that. Any sample code will highly be appreciated.




  2. #2
    Join Date
    Apr 1999
    Posts
    23

    Re: Member function access

    Show a piece of your code here and people will be of help.


  3. #3
    Guest

    Re: Member function access

    // .h file
    class test1 {
    func1();
    func2();
    }

    class test2 {
    friend class test1;
    funct2();
    funct3();
    }

    // cpp file

    test1::func1()
    {
    funct1() // I want just this but the vc compiler gives error
    // it doesn't know funct1()
    }


  4. #4
    Join Date
    Apr 1999
    Posts
    383

    Re: Member function access

    The compiler says it doesn't know funct1 because funct1 isn't declared anywhere!

    You declare func1 and func2 in test1, and funct2 and funct3 in test2, but no funct1.

    If funct1 is supposed to be a member of test2, you must call it on an instance of test2 (unless it is declared 'static'):

    class test2 {
    friend class test1;
    void funct1();
    void funct2();
    void funct3();
    };

    test1::func1()
    {
    test2 anInst; // create an instance of test2
    anInst.funct1(); // call the function
    }

    Incidentally, the class declarations must be terminated with a semi-colon ( after the curly braces, and so should the call to funct1. The member function declarations should be given return types also.

    Dave



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured