ambiguous << operator, overload not right?

This is a discussion on ambiguous << operator, overload not right? within the C++ Programming forums, part of the General Programming Boards category; I have two classes; a Term class, and a Poly class (which is just a list of terms). I wrote ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    ambiguous << operator, overload not right?

    I have two classes; a Term class, and a Poly class (which is just a list of terms). I wrote two overloaded << operators for displaying them, one takes in a term, the other takes in a poly. The term overloaded << is declared a friend in the term class, and the poly overloaded << is devlared a friend in the poly class. Any ideas what is wrong?

    Code:
    #include "term.h"
    #include "poly.h"
    
    ostream & operator << (ostream &disp, const Term &rhs)
    {
    	int c = rhs.getCoef();
    	int x = rhs.getExpn();
    	if (c == 0) { return disp; }
    	else if (c == 1) { disp << "X^" << x; }
    	else if (x == 0) { disp << c; }
    	else if (x == 1) { disp << c << "X"; }
    	else { disp << c << "X^" << x; }
    	return disp;
    }
    
    ostream & operator << (ostream &disp, const Poly &rhs)
    {
    	Iterator<Term> it(rhs);
    	it.seekStart();
    	do
    	{
    		disp << it.getData() << " ";   //I get this line amibiguous <<
    	} while (it.next());
    	return disp;
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Forgot to mention,
    Code:
    it.getData()
    returns a term.
    also, the exact error I get on that line is:
    Code:
    error C2593: 'operator <<' is ambiguous
    nothing else =/
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    ambiguous errors generally have very little to do with the function itself. The problem lies in how you are using it. It would be helpful to see how Term and Poly are declared. It would also be helpful to see the offending line of your code.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    The offending line is the one commented in the first post. Here are Poly and Term:

    Code:
    //term.h
    #ifndef _TERM_H
    #define _TERM_H
    
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    class Term
    {
    	private:
    		double coef;
    		int expn;
    	public:
    		Term() { set(0.0, 0); }
    		Term(double c, int x) { set(c, x); }
    		double getCoef() const { return coef; }
    		int getExpn() const { return expn; }
    		void setCoef(double c) { coef = c; }
    		void setExpn(int x) { expn = x; }
    		void set(double c, int x) { setCoef(c); setExpn(x); }
    		double eval(double x) const { return (coef * pow(x, expn)); }
    		friend ostream & operator << (ostream &, const Term &);
    };
    
    #endif
    Code:
    //poly.h
    #ifndef _POLY_H
    #define _POLY_H
    
    #include <iostream>
    #include "llist.h"
    #include "term.h"
    using namespace std;
    
    class Poly
    {
    	private:
    		LList<Term> P;
    	public:
    		Poly() {}
    		Poly(double c, int x) { Term temp(c,x); P.insert(temp); }
    		void addTerm(double c, int x)
    		{
    			Iterator<Term> it(P);
    			Term temp(c,x);
    			do
    			{
    				double cof = it.getData().getCoef();
    				int xpn = it.getData().getExpn();
    				if (xpn == x)
    				{
    					it.getData().setCoef(cof+c);
    					break;
    				}
    			} while (it.next());
    		}
    		void subTerm(double c, int x) { addTerm(-c, x); }
    		double solve(double x)
    		{ 
    			Term temp;
    			double total = 0;
    			Iterator<Term> it(P);
    			it.seekStart();
    			do
    			{
    				temp = it.getData();
    				total += temp.eval(x);
    
    			} while (it.next());
    			return total;
    		}
    		friend class Iterator<Term>;
    		friend ostream & operator << (ostream &, const Poly &);
    };
    
    #endif
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i don't know how you're trying to use the function, but for some reason i'm thinking friend functions need to be declared before any access identifiers (private, public, protected)...

    i very well could be wrong, but i remember having a similar problem with friend functions...

    don't get your hopes up, but try

    class xx
    {
    friend ostream whatever();
    public:
    private:
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    friends can be declared anywhere in the class defination.
    Ok I didn't realize where your ambigious was.
    Can I see Iterator also please.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ok, Iterator is used with my Linked List, so here they are (in same file):

    Code:
    //list.h
    #ifndef _LLIST_H
    #define _LLIST_H
    
    #include "node.h"
    #include <iostream>
    using namespace std;
    
    template <class T> class Iterator;
    
    template <class T>
    class LList
    {
    	private:
    		Node<T> *head;
    		Node<T> *curr;
    	public:
    		LList() { head = new Node<T>; curr = head; }
    		~LList() { seekStart(); while (remove()); }
    
    		bool insert(const T &d)
    		{
    			if (curr == NULL) return false;
    			Node<T> *temp = new Node<T>(d, curr->getNext());
    			curr->setNext(temp);
    			return true;
    		}
    		bool remove()
    		{
    			if (curr->getNext() == NULL) return false;
    			Node<T> *temp = curr->getNext();
    			curr->setNext(temp->getNext());
    			delete temp;
    			return true;
    		}
    		const T& getData() { return curr->getNext()->getItem(); }
    		void setData(const T &d) { curr->getNext()->setItem(d); }
    		bool isEmpty() { return head->getNext() == NULL; }
    		bool next()
    		{
    			if ((curr == NULL) || (curr->getNext() == NULL)) return false;
    			curr = curr->getNext();
    			return true;
    		}
    		bool prev()
    		{
    			if ((curr == NULL) || (curr == head)) return false;
    			Node<T> *temp = curr;
    			seekStart();
    			while (curr->getNext != temp)
    				next();
    			return true;
    		}
    		void seekStart() { curr = head; }
    		void seekEnd() { while (next()); }
    
    		LList<T> operator = (const LList<T> &rhs)
    		{
    			Iterator<T> it(rhs);
    			*this.head = rhs.head;
    			*this.curr = rhs.curr;
    			it.seekStart();
    			do
    			{
    				*this.insert(it.getData());
    			} while (it.next());
    		}
    		friend class Iterator<T>;
    };
    
    class Poly;
    
    template <class T>
    class Iterator
    {
    	private:
    		Node<T> *Pos;
    		const LList<T> *theList;
    	public:
    		Iterator() { return; }
    		Iterator(const LList<T> &l):theList(&l) { setList(); }
    		Iterator(const Poly &p):theList(&p.P) { setList(); }
    		~Iterator() { delete Pos; }
    		void setList()
    		{
    			Node<T> *Pos = new Node<T>;
    			Pos = theList->curr;
    		}
    		bool next()
    		{
    			if ((Pos == NULL) || (Pos->getNext() == NULL)) return false;
    			Pos = Pos->getNext();
    			return true;
    		}
    		bool prev()
    		{
    			if ((Pos == NULL) || (Pos == theList->head )) return false;
    			Node<T> *temp = Pos;
    			Pos = theList->head;
    			while (Pos->getNext() != temp)
    				Pos = Pos->getNext();
    			return true;
    		}
    		void seekStart() { Pos = theList->curr; }
    		T& getData() { return Pos->getItem(); }
    		void setData(T &d) { Pos->getNext()->setItem(d); }
    };
    
    #endif
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Going through working on the project, and I'm beginning to think that there's something wrong with the Iterator class. Does the deconstructor do its thing right? I'm getting crashes sometimes when using iterators.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  3. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM
  4. Ambiguous << error
    By PJYelton in forum C++ Programming
    Replies: 8
    Last Post: 01-13-2003, 03:40 PM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 03:07 PM

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