Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Friend-function

cuongmitscuongmits Posts: 34Member
[b][red]This message was edited by cuongmits at 2006-3-27 11:31:57[/red][/b][hr]
Help me to fix this error with friend-function please
how to use method A::get(int b_) in class B?
Thks.

[code]
#include
class A
{
public:
int get1(int b_){return b_;}
};
class B
{
int b;
public:
B(){b=1;}
out(){cout << b << endl;}
friend A::get(int b_);
void get2(int b_){b=A::get1(int b_);}//error here?
//how to use method A::get(int b_) here?
};
void main()
{
B b1;
b1.out(); //1
b1.get2(100); //
b1.out(); //100
}
[/code]


Comments

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : [b][red]This message was edited by cuongmits at 2006-3-27 11:31:57[/red][/b][hr]
    : Help me to fix this error with friend-function please
    : how to use method A::get(int b_) in class B?
    : Thks.
    :
    : [code]
    : #include
    : class A
    : {
    : public:
    : int get1(int b_){return b_;}
    : };
    : class B
    : {
    : int b;
    : public:
    : B(){b=1;}
    : out(){cout << b << endl;}
    : friend A::get(int b_);
    : void get2(int b_){b=A::get1(int b_);}//error here?
    : //how to use method A::get(int b_) here?
    : };
    : void main()
    : {
    : B b1;
    : b1.out(); //1
    : b1.get2(100); //
    : b1.out(); //100
    : }
    : [/code]
    :
    :

    [blue]To call a member function like you are trying to do, you either need to use an actual instance of the A class [b]or[/b] you need to make the function [italic]static[/italic]... and I'm guessing you want to go the [italic]static[/italic] route.[/blue]

  • cuongmitscuongmits Posts: 34Member
    [b][red]This message was edited by cuongmits at 2006-3-27 13:17:34[/red][/b][hr]
    : : [b][red]This message was edited by cuongmits at 2006-3-27 11:31:57[/red][/b][hr]
    : : Help me to fix this error with friend-function please
    : : how to use method A::get(int b_) in class B?
    : : Thks.
    : :
    : : [code]
    : : #include
    : : class A
    : : {
    : : public:
    : : int get1(int b_){return b_;}
    : : };
    : : class B
    : : {
    : : int b;
    : : public:
    : : B(){b=1;}
    : : out(){cout << b << endl;}
    : : friend A::get(int b_);
    : : void get2(int b_){b=A::get1(int b_);}//error here?
    : : //how to use method A::get(int b_) here?
    : : };
    : : void main()
    : : {
    : : B b1;
    : : b1.out(); //1
    : : b1.get2(100); //
    : : b1.out(); //100
    : : }
    : : [/code]
    : :
    : :
    :
    : [blue]To call a member function like you are trying to do, you either need to use an actual instance of the A class [b]or[/b] you need to make the function [italic]static[/italic]... and I'm guessing you want to go the [italic]static[/italic] route.[/blue]
    :
    :
    I dont know what you mean. That code is only for example, but I have no idea to fix it :(
    Thank you.


  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : [b][red]This message was edited by cuongmits at 2006-3-27 13:17:34[/red][/b][hr]
    : : : [b][red]This message was edited by cuongmits at 2006-3-27 11:31:57[/red][/b][hr]
    : : : Help me to fix this error with friend-function please
    : : : how to use method A::get(int b_) in class B?
    : : : Thks.
    : : :
    : : : [code]
    : : : #include
    : : : class A
    : : : {
    : : : public:
    : : : int get1(int b_){return b_;}
    : : : };
    : : : class B
    : : : {
    : : : int b;
    : : : public:
    : : : B(){b=1;}
    : : : out(){cout << b << endl;}
    : : : friend A::get(int b_);
    : : : void get2(int b_){b=A::get1(int b_);}//error here?
    : : : //how to use method A::get(int b_) here?
    : : : };
    : : : void main()
    : : : {
    : : : B b1;
    : : : b1.out(); //1
    : : : b1.get2(100); //
    : : : b1.out(); //100
    : : : }
    : : : [/code]
    : : :
    : : :
    : :
    : : [blue]To call a member function like you are trying to do, you either need to use an actual instance of the A class [b]or[/b] you need to make the function [italic]static[/italic]... and I'm guessing you want to go the [italic]static[/italic] route.[/blue]
    : :
    : :
    : I dont know what you mean. That code is only for example, but I have no idea to fix it :(
    : Thank you.
    :
    :

    [code]
    #include <[blue]iostream[/blue]> [blue]// Using [italic]iostream[/italic] vs [italic]iostream.h[/italic] is preferable[/blue]

    class A
    {
    public:
    [blue]static[/blue] int get1(int b_){return b_;}
    };

    class B
    {
    int b;
    public:
    B(){b=1;}
    [blue]void[/blue] out(){[blue]std::[/blue]cout << b << [blue]std::[/blue]endl;}
    void get2(int b_){b=A::get1(int b_);}
    };

    [blue]int[/blue] main()
    {
    B b1;
    b1.out(); //1
    b1.get2(100); //
    b1.out(); //100

    [blue]return 0;[/blue]
    }
    [/code]

  • cuongmitscuongmits Posts: 34Member
    : : [b][red]This message was edited by cuongmits at 2006-3-27 13:17:34[/red][/b][hr]
    : : : : [b][red]This message was edited by cuongmits at 2006-3-27 11:31:57[/red][/b][hr]
    : : : : Help me to fix this error with friend-function please
    : : : : how to use method A::get(int b_) in class B?
    : : : : Thks.
    : : : :
    : : : : [code]
    : : : : #include
    : : : : class A
    : : : : {
    : : : : public:
    : : : : int get1(int b_){return b_;}
    : : : : };
    : : : : class B
    : : : : {
    : : : : int b;
    : : : : public:
    : : : : B(){b=1;}
    : : : : out(){cout << b << endl;}
    : : : : friend A::get(int b_);
    : : : : void get2(int b_){b=A::get1(int b_);}//error here?
    : : : : //how to use method A::get(int b_) here?
    : : : : };
    : : : : void main()
    : : : : {
    : : : : B b1;
    : : : : b1.out(); //1
    : : : : b1.get2(100); //
    : : : : b1.out(); //100
    : : : : }
    : : : : [/code]
    : : : :
    : : : :
    : : :
    : : : [blue]To call a member function like you are trying to do, you either need to use an actual instance of the A class [b]or[/b] you need to make the function [italic]static[/italic]... and I'm guessing you want to go the [italic]static[/italic] route.[/blue]
    : : :
    : : :
    : : I dont know what you mean. That code is only for example, but I have no idea to fix it :(
    : : Thank you.
    : :
    : :
    :
    : [code]
    : #include <[blue]iostream[/blue]> [blue]// Using [italic]iostream[/italic] vs [italic]iostream.h[/italic] is preferable[/blue]
    :
    : class A
    : {
    : public:
    : [blue]static[/blue] int get1(int b_){return b_;}
    : };
    :
    : class B
    : {
    : int b;
    : public:
    : B(){b=1;}
    : [blue]void[/blue] out(){[blue]std::[/blue]cout << b << [blue]std::[/blue]endl;}
    : void get2(int b_){b=A::get1(int b_);}
    : };
    :
    : [blue]int[/blue] main()
    : {
    : B b1;
    : b1.out(); //1
    : b1.get2(100); //
    : b1.out(); //100
    :
    : [blue]return 0;[/blue]
    : }
    : [/code]
    :
    :
    Have you compiled it? I have gotten so many errors when I compiled it.
    And the most important thing here that I want to call a friend-function from a class, that function is declared in another class.
    Anyway thank you HK_MP5KPDW.

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : : : : : Help me to fix this error with friend-function please
    : : : : : how to use method A::get(int b_) in class B?
    : : : : : Thks.
    : : : : :
    : : : : : [code]
    : : : : : #include
    : : : : : class A
    : : : : : {
    : : : : : public:
    : : : : : int get1(int b_){return b_;}
    : : : : : };
    : : : : : class B
    : : : : : {
    : : : : : int b;
    : : : : : public:
    : : : : : B(){b=1;}
    : : : : : out(){cout << b << endl;}
    : : : : : friend A::get(int b_);
    : : : : : void get2(int b_){b=A::get1(int b_);}//error here?
    : : : : : //how to use method A::get(int b_) here?
    : : : : : };
    : : : : : void main()
    : : : : : {
    : : : : : B b1;
    : : : : : b1.out(); //1
    : : : : : b1.get2(100); //
    : : : : : b1.out(); //100
    : : : : : }
    : : : : : [/code]
    : : : : :
    : : : : :
    : : : :
    : : : : [blue]To call a member function like you are trying to do, you either need to use an actual instance of the A class [b]or[/b] you need to make the function [italic]static[/italic]... and I'm guessing you want to go the [italic]static[/italic] route.[/blue]
    : : : :
    : : : :
    : : : I dont know what you mean. That code is only for example, but I have no idea to fix it :(
    : : : Thank you.
    : : :
    : : :
    : :
    : : [code]
    : : #include <[blue]iostream[/blue]> [blue]// Using [italic]iostream[/italic] vs [italic]iostream.h[/italic] is preferable[/blue]
    : :
    : : class A
    : : {
    : : public:
    : : [blue]static[/blue] int get1(int b_){return b_;}
    : : };
    : :
    : : class B
    : : {
    : : int b;
    : : public:
    : : B(){b=1;}
    : : [blue]void[/blue] out(){[blue]std::[/blue]cout << b << [blue]std::[/blue]endl;}
    : : void get2(int b_){b=A::get1(int b_);}
    : : };
    : :
    : : [blue]int[/blue] main()
    : : {
    : : B b1;
    : : b1.out(); //1
    : : b1.get2(100); //
    : : b1.out(); //100
    : :
    : : [blue]return 0;[/blue]
    : : }
    : : [/code]
    : :
    : :
    : Have you compiled it? I have gotten so many errors when I compiled it.
    : And the most important thing here that I want to call a friend-function from a class, that function is declared in another class.
    : Anyway thank you HK_MP5KPDW.
    :

    [blue]You mean have I compiled the corrected example I provided you? Yes I have. I've even tested it a couple times. Post the errors you are getting.

    You don't need to use friend classes for the example you provided. You only need to use friend classes if you want to access another classes non-public member function/variables which does not apply to the example you provided. The [italic]A::get1[/italic] function is declared as public so you do not need to declare B as a friend class. You could make it private instead in which case you would need to use a friend statement in class A (not B).[/blue]

    [code]
    #include <iostream>

    class A
    {
    [blue]// Make this private instead of public[/blue]
    static int get1(int b_){return b_;}
    [blue]// Make this class a friend of B so B can access its privates
    friend class B;[/blue]
    };

    class B
    {
    int b;
    public:
    B(){b=1;}
    void out(){std::cout << b << std::endl;}
    void get2(int b_){b=A::get1(int b_);}
    };

    int main()
    {
    B b1;
    b1.out(); //1
    b1.get2(100);
    b1.out(); //100

    return 0;
    }
    [/code]

  • cuongmitscuongmits Posts: 34Member
    : : : : : : Help me to fix this error with friend-function please
    : : : : : : how to use method A::get(int b_) in class B?
    : : : : : : Thks.
    : : : : : :
    : : : : : : [code]
    : : : : : : #include
    : : : : : : class A
    : : : : : : {
    : : : : : : public:
    : : : : : : int get1(int b_){return b_;}
    : : : : : : };
    : : : : : : class B
    : : : : : : {
    : : : : : : int b;
    : : : : : : public:
    : : : : : : B(){b=1;}
    : : : : : : out(){cout << b << endl;}
    : : : : : : friend A::get(int b_);
    : : : : : : void get2(int b_){b=A::get1(int b_);}//error here?
    : : : : : : //how to use method A::get(int b_) here?
    : : : : : : };
    : : : : : : void main()
    : : : : : : {
    : : : : : : B b1;
    : : : : : : b1.out(); //1
    : : : : : : b1.get2(100); //
    : : : : : : b1.out(); //100
    : : : : : : }
    : : : : : : [/code]
    : : : : : :
    : : : : : :
    : : : : :
    : : : : : [blue]To call a member function like you are trying to do, you either need to use an actual instance of the A class [b]or[/b] you need to make the function [italic]static[/italic]... and I'm guessing you want to go the [italic]static[/italic] route.[/blue]
    : : : : :
    : : : : :
    : : : : I dont know what you mean. That code is only for example, but I have no idea to fix it :(
    : : : : Thank you.
    : : : :
    : : : :
    : : :
    : : : [code]
    : : : #include <[blue]iostream[/blue]> [blue]// Using [italic]iostream[/italic] vs [italic]iostream.h[/italic] is preferable[/blue]
    : : :
    : : : class A
    : : : {
    : : : public:
    : : : [blue]static[/blue] int get1(int b_){return b_;}
    : : : };
    : : :
    : : : class B
    : : : {
    : : : int b;
    : : : public:
    : : : B(){b=1;}
    : : : [blue]void[/blue] out(){[blue]std::[/blue]cout << b << [blue]std::[/blue]endl;}
    : : : void get2(int b_){b=A::get1(int b_);}
    : : : };
    : : :
    : : : [blue]int[/blue] main()
    : : : {
    : : : B b1;
    : : : b1.out(); //1
    : : : b1.get2(100); //
    : : : b1.out(); //100
    : : :
    : : : [blue]return 0;[/blue]
    : : : }
    : : : [/code]
    : : :
    : : :
    : : Have you compiled it? I have gotten so many errors when I compiled it.
    : : And the most important thing here that I want to call a friend-function from a class, that function is declared in another class.
    : : Anyway thank you HK_MP5KPDW.
    : :
    :
    : [blue]You mean have I compiled the corrected example I provided you? Yes I have. I've even tested it a couple times. Post the errors you are getting.
    :
    : You don't need to use friend classes for the example you provided. You only need to use friend classes if you want to access another classes non-public member function/variables which does not apply to the example you provided. The [italic]A::get1[/italic] function is declared as public so you do not need to declare B as a friend class. You could make it private instead in which case you would need to use a friend statement in class A (not B).[/blue]
    :
    : [code]
    : #include <iostream>
    :
    : class A
    : {
    : [blue]// Make this private instead of public[/blue]
    : static int get1(int b_){return b_;}
    : [blue]// Make this class a friend of B so B can access its privates
    : friend class B;[/blue]
    : };
    :
    : class B
    : {
    : int b;
    : public:
    : B(){b=1;}
    : void out(){std::cout << b << std::endl;}
    : void get2(int b_){b=A::get1(int b_);}
    : };
    :
    : int main()
    : {
    : B b1;
    : b1.out(); //1
    : b1.get2(100);
    : b1.out(); //100
    :
    : return 0;
    : }
    : [/code]
    :
    : Errors for code you have posted:
    (16,3) qualifier std is not a class or namespace name
    (16,3) Statement missing ;
    (17,3) Expression syntax
    (16,14) qualifier std is not a class or namespace name
    (16,14) statment missing ;
    (17,18) Expression syntax
    (16,14) qualifier std is not a class or namespace name
    (16,14) statement missing ;
    I use BC++ 5.0
    Can you give me an example for friend-function (how to use it in method of another class?)
    Thank so much.

  • cuongmitscuongmits Posts: 34Member
    : [b][red]This message was edited by cuongmits at 2006-3-27 11:31:57[/red][/b][hr]
    : Help me to fix this error with friend-function please
    : how to use method A::get(int b_) in class B?
    : Thks.
    :
    : [code]
    : #include
    : class A
    : {
    : public:
    : int get1(int b_){return b_;}
    : };
    : class B
    : {
    : int b;
    : public:
    : B(){b=1;}
    : out(){cout << b << endl;}
    : friend A::get(int b_);
    : void get2(int b_){b=A::get1(int b_);}//error here?
    : //how to use method A::get(int b_) here?
    : };
    : void main()
    : {
    : B b1;
    : b1.out(); //1
    : b1.get2(100); //
    : b1.out(); //100
    : }
    : [/code]
    :
    :
    :
    How to do it, please help me!!!!
Sign In or Register to comment.