C++ Class Function/Method Friends?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcheelol
    New Member
    • Feb 2010
    • 25

    C++ Class Function/Method Friends?

    i'm trying to use the method friends in this class 3...here is the error im getting.

    Error 2316 112: 'Host3::seeData (Class3)' is not a member of 'Host3'

    Code:
    class Host3;
    class Class3: public Class2{
    protected:
    int *p5;
    public:
    Class3();
    ~Class3();
    void inputData();
    void showData();
    friend int Host3::seeData(Class3 ); -----------$$$$$$$$$$$$-------LINE 112~~~~~
    }; // Class3
    
    Class3::Class3(){
    cout << "\n\n\tClass3 Constructor";
    p5 = new int;
    }
    Class3::~Class3(){
    cout << "\n\n\tClass3 Destructor";
    delete p5;
    }
    void Class3::inputData(){
    cout << "\n\tEnter a character >> ";
    cin >> *p1;
    cout << "\n\tEnter a float >> ";
    cin >> *p2;
    cout << "\n\tEnter a string >> ";
    cin >> *p4;
    cout << "\n\tEnter an integer >> ";
    cin >> *p5;
    } 
    void Class3::showData(){
    cout << "\n\n\t" << *p2 << " is the float";
    cout << "\n\n\t" << *p4 << " is the string";
    cout << "\n\n\t" << *p5 << " is the integer";
    }
    
    class Host3{
    public:
    Host3(){cout<<"\n\tHost3C";}
    ~Host3(){cout<<"\n\tHost3D";}
    int seeData(Class3 );
    };
    
    Host3::seeData(Class3 ){
    cout << "\n\tEnter a character >> ";
    cin >> *p1;
    cout << "\n\tEnter a float >> ";
    cin >> *p2;
    cout << "\n\tEnter a string >> ";
    cin >> *p4;
    cout << "\n\tEnter an integer >> ";
    cin >> *p5;
    return 0;
    }
    thats my source code, i'm not really sure what im doing wrong.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    At the point in the file, line 112, that the friend statement appears on a forward declaration fro Hst3 has been seen by the compiler not a full definition so the compiler is unaware of any members of Host3 and you can not reference any of them.

    To make that friend declaration the compiler needs to have seen the full definition of Host3 first.

    Comment

    • lolcheelol
      New Member
      • Feb 2010
      • 25

      #3
      if i do that, then class 3 wouldn't be recognized in the Host3 class.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Perhaps you should pass a Class3& to Host3::seeData then Host3 only requires a forward declaration of Class3

        Comment

        • lolcheelol
          New Member
          • Feb 2010
          • 25

          #5
          yeah im still getting that one same error...

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You will need to do all of the following

            pass a Class3& to Host3::seeData
            forward declare Class3 before Host3
            declare Host3 before Class3
            declare Class3 before defining Host3::seeData if this method actually uses Class3

            Comment

            Working...