Thread: friend question

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    friend question

    i want to make class A friend of class B, so when i declare an instance of A within B its private members will also in handy. but things go wrongly when i compile it, my code is right below:
    Code:
    template <class T>
    class A
    {
    	friend B<T>;
    	public:
    		//
    	private:
    		T data;
    };
    template <class T>
    class B
    {
    	public :
    		//
    	private:
    		A<T>* a;
    }
    i just can not figure out the problem, any ideas ?

    btw, i am with Dev-C++ 5, thanx.
    Last edited by black; 08-02-2004 at 03:05 AM.
    Never end on learning~

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    friend B<T>;
    T data;
    A doesn't know what a T is.
    Code:
    private: A<T>* a;
    A isn't templated

    You need to either tell what kind of template B needs to be by giving it a known type and create that member data so that it is of that chosen type, or template A itself. Also, by the looks of what you're trying to do, you might want to try out private inheritance, although it may not be necessary.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    sorry i forgot to add template line above class A, even i add that line my compiler will still report an error, any suggestions ?
    Never end on learning~

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    well, A need's to know what class B looks like since it has the friend declaration, so B needs to be declared before A....but...B needs to know what A looks like since it has A as a private member, so A needs to go first. Curse you circular dependance!

    if you put a forward declarations of B infront of A, all should be fine. It just tells the compiler that B is indeed a class, but it's defined somewhere else.
    Code:
    template <class T> class B;
    By the way, it's become standard practice to use template <typename> instead of <class>, but to my knowledge there is no difference.

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    oh thanx let me try it. but it is strange that my book didnt mention anything about this, mmm...
    Never end on learning~

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    my god i tried that but failed either. these are what my code looks like:
    Code:
    template <class T>
    class Stack;
    template <class T>
    class StackNode
    {
    	friend Stack<T>;
    	public :
    		//
    	private :
    		T __data;
    		StackNode* __next;
    };
    template <class T>
    class Stack
    {
        //
    };
    what's wrong again ???
    Never end on learning~

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Nothing seems to be wrong, and that snippet compiled fine on my machine. If it's not the same error, I ask that you post what the error messages are. Other than that, I can only suggest that you make StackNode a struct instead of a class with private fields, making the friend declaration unecessary. It wouldn't solve the problem, merely get around it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question about a use a friend class
    By dorenthe in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 12:38 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:37 PM