**STUPID** newbie question...

This is a discussion on **STUPID** newbie question... within the C++ Programming forums, part of the General Programming Boards category; Hi! Can anyone plz tell me why the following code does not work? Code: class Class2; class Class1 { friend ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Unhappy **STUPID** newbie question...

    Hi!

    Can anyone plz tell me why the following code does not work?

    Code:
    class Class2;
    
    class Class1
    {
     friend void Class2::func2();
    
     private:
    	void func1(){}
    };
    
    class Class2
    {
     public:
    	void func2(){}
    };
    I get an error message saying that Class2::func2() is not a member of Class2..!!!???

    thanx

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,647
    Limitations of forward declaration:
    - You can only use it if the size of the forwarded declaration is not needed
    - You can not access any elements of the forwarded type

    Declare Class2 first.

    gg

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Unhappy Still don't get it... (no answer so far...)

    Yes, I am sure that would work. However, there is more...
    Take a look at the code below:
    Code:
    class Class2;
    
    class Class1
    {
     friend void Class2::func2();
    
     private:
    	void func1(){}
    };
    
    class Class2
    {
     public:
    	void func2();
    };
    
    void Class2::func2()
    {
     Class1 c1;
     c1.func1();
    }
    I get the same error as before, Class1 can't find the function in Class2. How can I solve this??
    If I can't, how can I have two classes access each others members?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,647
    Declare Class2 first - you don't have to implement it first.

    Code:
    class Class2
    {
     public:
    	void func2();
    };
    
    class Class1
    {
     friend void Class2::func2();
    
     private:
    	void func1(){}
    };
    
    void Class2::func2()
    {
     Class1 c1;
     c1.func1();
    }
    gg

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Thanx that works

    Code:
    int main()
    {
      char* a = "Thanx that works!";
      cout << a;
    
     return 0;
    }

  6. #6
    cliio
    Guest

    Question ??

    you are declaring an object of a class called Class2 which is the same name as the class, i dont think that you can do that.

    maybe you should try this
    class2 mpClass2; // instead of 'Class Class2;


    class Class1
    {
    public: // you should always say that it is public r it
    //be seen as a private data function or member
    friend void Class2::func2();

    private:
    void func1(){}
    };

    class Class2
    {
    public:
    void func2(){}
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 03:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 03:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21