Thread: class problem

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    30

    class problem

    i declared 2 class, and with a function from one class i want to access the data in the other class but im not sure how this can be done. when i tried, i kept on getting error messages.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can make the class you need to access a friend of the class that is doing the accessing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    can u give an example code?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can u give an example code?
    Code:
    class myclass {
      friend class some_class;
      ...
    }
    My best code is written with the delete key.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    How to declare a function as friend

    The following code allows a member function from one class to access the other class

    Code:
    #include <friendclass.h>
    
    Class Myclass
    {
        public:
    
        private:
    
    
    friend friendclass::friendfunction(Myclass Aclass);
    };
    if you want all the member functions of one class to access the other then include friend friendclass; in the Class definition.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    class myclass {
      friend class some_class;
      ...
    Does this allow all methods of some_class access all members of myclass, even private members of someclass?

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    30
    er.... this code dont work
    Code:
     #include <iostream.h>
    
    class myclass {
       friend class yourclass;
    	protected:
       	int variable;
       public:
       	myclass();
    };
    
    myclass :: myclass()
    {
    	variable = 1;
    }
    
    class yourclass {
    	public:
       	yourclass();
    };
    
    yourclass :: yourclass()
    {
    	cout << access.variable;
    }
    
    int main()
    {
    	myclass access;
       yourclass access2;
       return 0;
    }
    i get an message keep saying that Undefined SYmbol "access"
    if i take off access and just leave variable, it says undefined symbol "variable"
    Last edited by Chobo_C++; 03-16-2004 at 05:48 PM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    That's because yourclass has no idea what myclass variables you've declared in main.
    Code:
    #include <iostream>
    
    using namespace std;
    
    class myclass {
      friend class yourclass;
    protected:
      int variable;
    public:
      myclass();
    };
    
    myclass :: myclass()
    {
      variable = 1;
    }
    
    class yourclass {
    public:
      yourclass ( myclass& );
    };
    
    yourclass :: yourclass ( myclass& access )
    {
      cout << access.variable;
    }
    
    int main()
    {
      myclass access;
      yourclass access2 ( access );
      return 0;
    }
    My best code is written with the delete key.

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    If you make the entire class a friend then yes.

    originally posted by elad
    Does this allow all methods of some_class access all members of myclass, even private members of someclass?
    If you make the entire class a friend then all the member functions have access to all the data including private members yes. A friend function is the same as an ordinary function except that it can access all the members of a class without restriction.
    Last edited by manofsteel972; 03-17-2004 at 11:52 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM