problems overloading =

This is a discussion on problems overloading = within the C++ Programming forums, part of the General Programming Boards category; For some reason every time I try to make an operator function a friend to a class, I always get ...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    problems overloading =

    For some reason every time I try to make an operator function a friend to a class, I always get the error "illegal operator declaration". I have no clue why. I would be more thankful than you would ever believe if you could solve my problem. Here's the code:

    Code:
    class intarr
    {
    protected:
    	struct node
    	{
    		int data;
    		node * next;
    	};
    	node * head;
    	node * current;
    public:
    	intarr() : head(0), current(head) {}
    	intarr(int *, unsigned int);
    	node & operator[](unsigned int);
    	friend node operator=(node &, int); //the problem
    	unsigned int len;
    };
    intarr::intarr(int * d, unsigned int l)
    {
    	head = new node;
    	current = head;
    	for(unsigned int a = 0; a < l; a++)
    	{
    		current->data = d[a];
    		current->next = new node;
    		current = current->next;
    	}
    }
    intarr::node & intarr::operator[](unsigned int i)
    {
    	current = head;
    	for (unsigned int a = 0; a < i; a++)
    		current = current->next;
    	return *current;
    }
    intarr::node operator=(intarr::node & n, int num)
    {
    	n.data = num;
    	return n;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    friend functions are not members of the class therefore leave off the class name and scope operator when defining them.

    intarr::node operator=(intarr::node & n, int num)

    should be

    node operator=(intarr::node & n, int num)

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by elad
    friend functions are not members of the class therefore leave off the class name and scope operator when defining them.
    he didn't do that -- you took the intar:: off the node type, which would give you the wrong type. node is a nested type inside the intarr class.

    The problem is that operator= (along with several other operators) is simply not allowed to be overloaded as a function, it has to be a member function of the class.

  4. #4
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    Thanks man, I totally forgot about that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems overloading outstream for custom ADT
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2005, 10:28 AM
  2. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 05:10 PM
  3. overloading << problems
    By rozner in forum C++ Programming
    Replies: 3
    Last Post: 10-27-2003, 12:27 PM
  4. New to board problems with overloading
    By Turbo02 in forum C++ Programming
    Replies: 21
    Last Post: 12-18-2002, 07:38 PM
  5. Problems: Operator overloading.
    By Dual-Catfish in forum C++ Programming
    Replies: 17
    Last Post: 06-18-2002, 07:38 PM

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