Problem with friend functions in templated linked list

This is a discussion on Problem with friend functions in templated linked list within the C++ Programming forums, part of the General Programming Boards category; I can't figure out why I keep getting a syntax error from these functions Code: NODE.H //holds a value. no ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    46

    Problem with friend functions in templated linked list

    I can't figure out why I keep getting a syntax error from these functions

    Code:
    NODE.H   //holds a value. no problems with this class
    -------------------------------
    
    #ifndef NODE_H
    #define NODE_H
    #include <cstdlib>
    
    template <class Item>
    class node
    {
      public:
        node(Item the_data = Item(), node* link_field = NULL)
        {data = the_data; link = link_field;}
        void set_link(node* link_field) {link = link_field;}
        void set_data(Item value) {data = value;}
        Item get_data() {return data;}
        node* get_link() {return link;}
        const Item get_data() const {return data;}
        const node* get_link() const {return link;}
      private:
        Item data;
        node* link;
    };
    #endif
    
    BAG.H //linked list class, holds a set
    -----------------------------------------------
    #ifndef BAG_H
    #define BAG_H
    #include <cstdlib>
    
    template <class Item>
    class bag
    {
      public:
        bag() {head = NULL;}
        void print();
        void input();
        void set_head(node<Item>* n1) {head = n1;}
        node<Item>* get_head() {return head;}
        const node<Item>* get_head() const {return head;}
        bool is_element(Item value);
        bag get_union(bag b1);
      
        //here's what my friend functions look like, don't know why
        //they don't work
        friend bool is_subset <>(const bag& b1, const bag& b2);
        friend bag get_difference <>(const bag& b1, const bag& b2);
        friend bag get_intersect <>(const bag& b1, const bag& b2);
        
       
      private:
        node<Item>* head;
    };
    it's divided up into four files. node.h (header containing inline functions of node class), bag.h(header file for bag class), bag.template (implementation for bag class), and main.cc (application file)
    and here's what the error message I get looks like:
    Code:
    bag.h:40: error: 'is_subset' is neither function nor member function; cannot be declared friend
    bag.h:40: error: expected ';' before '<' token
    the error is the same with get_difference and get_intersect. I can't figure out what I'm doing wrong

    I don't think you need to see the definitions for this, but if you do let me know

    EDIT: I'm using the g++ compiler on a unix operating system
    Last edited by Strait; 03-12-2005 at 04:04 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    help plesae

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Here is an example:

    Code:
    #include <iostream>
    
    template <class T>
    class A
    {
        T item;
    public:
        A(const T& val) : item(val) {}
        friend std::ostream& operator<<(std::ostream&,const A<T>&);
    };
    
    template<class T>
    std::ostream& operator<<(std::ostream& os,const A<T>& a)
    {
        return os << a.item;
    }
    
    int main()
    {
        A<int> a(500);
        A<float> b(500.456f);
    
        std::cout << a << std::endl;
        std::cout << b << std::endl;
    
        return 0;
    }
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  2. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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