9 Replies - 834 Views - Last Post: 16 June 2013 - 05:56 PM Rate Topic: -----

#1 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

no match for 'operator<'.....

Posted 16 June 2013 - 09:43 AM

Hey all I am getting this error when I compile my code. I used to have two errors one being for operator+ but a friend kindly helped me fix that (line 56 in polynomial.cpp). I was wondering if you could shed some light on this situation.

#include <cmath> // re. pow( x, exp )
#include "polynomial.h"



Polynomial::Polynomial () : order(0) {} // construct a 'zero' poly...

Polynomial::Polynomial ( int size, const double coeff[] )
{
  for( int i = 0; i < size; ++i )
  {
  	if( coeff[i] )
  	{
  	  Term tmp( coeff[i], i );
  	  coefficients.push_back( tmp );
  	}
  }

  if( coefficients.size() )
  	order = coefficients.back().power;
  else order = 0;
}

Polynomial::Polynomial ( int nCoeff, double c ) : order(nCoeff-1)
{
  if( c )
  {
    for( int i = 0; i < nCoeff; ++i )
    {
  	  Term tmp( c, i );
  	  coefficients.push_back( tmp );
    }
  }

  if( coefficients.size() )
  	order = coefficients.back().power;
  else order = 0;
}

double Polynomial::evaluate ( double x ) const
{
  double sum = 0.0;
  std::list< Term >::const_iterator it;
  for( it = coefficients.begin(); it !=  coefficients.end(); ++it )
    sum += it->coefficient * pow( x, it->power );
  return sum;
}

int Polynomial::getOrder() const
{
  return order;
}


Polynomial Polynomial::operator + ( const Polynomial& p ) const
{
  Polynomial result; // calls default ctor ... order set to zero
  std::list< Term >::const_iterator a = coefficients.begin(),
  									aend = coefficients.end(),
									b = p.coefficients.begin(),
									bend = p.coefficients.end();

  while( a != aend && b != bend )
  {
  	if( *a < *b ) // comparing powers only
  	{
  	  result.coefficients.push_back( *a );
  	  ++a;
  	}
  	else if( *a == *b ) // comparing powers only
  	{
  	  if( a->coefficient != - b->coefficient )
  	  {
  	  	Term tmp( *a );
  	  	tmp.coefficient += b->coefficient;
  	  	result.coefficients.push_back( tmp ); // a non zero term //
  	  }
  	  ++a;
	  ++b;
  	}
  	else
  	{
  	  result.coefficients.push_back( *b );
  	  ++b;
  	}
  }

  // at most one of the following two wile loops gets executed
  while( a != aend )
  {
  	result.coefficients.push_back( *a );
  	++a;
  }

  while( b != bend )
  {
  	result.coefficients.push_back( *b );
  	++b;
  }

  //result.checkOrder(); // redundant here since only NON zero terms appeneded above //
  if( result.coefficients.size() )
  	result.order = result.coefficients.back().power;
  // else power was set by ctor above to zero

  return result;
}


Polynomial Polynomial::operator * ( double scale ) const
{
  if( scale ) // if NOT zero
  {
	Polynomial result (*this);

	std::list< Term >::iterator it;
	for( it = result.coefficients.begin(); it != result.coefficients.end(); ++it )
	  it->coefficient *= scale;
	return result;
  }
  // else ...
  return Polynomial(); // return a ZERO poly...
}


void Polynomial::checkOrder ()
{
  while( coefficients.back().coefficient == 0 ) coefficients.pop_back();
  if( coefficients.size() )
    order = coefficients.back().power;
  else order = 0;
}


std::istream& operator >> (std::istream& in, Polynomial& p)
{
  int r = 0; // r is number of coefficents and must be >= 1
  p.coefficients.clear();

  if( in >> r  &&  r  > 0 )
  {
    for( int i = 0; i < r; ++i )
    {
      double tmp;
      if( ! ( in >> tmp ) )
      {
    	std::cout << "Please start over and enter the " << r << " decimal coeff's:\n";
    	in.clear();
    	in.sync();
    	p.coefficients.clear();
    	i = 0;
      }
      else
      {
      	if( tmp )
      	{
      		Term tmpTerm;
      		tmpTerm.coefficient = tmp;
      		p.order = tmpTerm.power = i;
      		p.coefficients.push_back( tmpTerm );
      	}
      }
    }

    // ok ... reached here ... so ...
    //p.checkOrder(); // redundant here since only NON zero terms appended above //
  }
  else
  {
    std::cout << "\nEnter a valid integer >= 1 for number of coefficents.\n";
  }

  return in;
}


std::ostream& operator << (std::ostream& out, const Polynomial& p)
{
  if( p.coefficients.size() == 0 ) return out << 0; // zero poly ...

  // else ...
  int count = 0;

  std::list< Term >::const_iterator it;
  for ( it = p.coefficients.begin(); it != p.coefficients.end(); ++it )
  {
    if ( count )  // Don't have an extra " + " sign on first  term ...
	{
	  out << " + ";
	}
	++count;

	/*
    out << it->coefficient; // Ok ... now print value ...

    if ( it->power  > 0 ) // Don't have an 'x' on first 'ZERO degree' term ...
	{
	  out << " x";
	  if ( it->power  > 1 ) // if deg higher than 1 ... show ^deg
	  {
	    out << "^" << it->power;
	  }
	}
	*/
	out << *it;
  }
  return out;
}


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

//#include <iostream> // now inclued by file "term.h" included below ...
#include <list>
#include "term.h"

class Polynomial
{
public:
  // Create a simple polonomial
  Polynomial ();

  // Create a poly from an array with 'size' coefficents
  Polynomial ( int size, const double ary[] );

  // Create a polynomial with nCoeff coefficients,
  // all set equal to c.
  //
  // E.g.,
  //   Polynomial p (3, 1.0);
  // creates a polynomial p representing: 1.0 + 1.0*x + 1.0*x^2
  //
  Polynomial (int nCoeff, double c);


  // Compute the value of this polynomial at a given value of x.
  // E.g.,
  //   double c[3] = {1.0, 2.0, 3.0};
  //   Polynomial p (3, c);
  //   cout << p.evaluate(2.0);
  // will print 17.0
  double evaluate (double x) const;

  // The order of a polynomial is the largest exponent of x with a
  // non-zero coefficient. E.g., for the sample polynomial used
  // above, the order is 2.
  int getOrder() const;

  // Add two polynomials, returning the polynomial representing
  // their sum.
  Polynomial operator+ (const Polynomial& p) const;

  // Multiply a polynomial by a floating point value.
  Polynomial operator* (double scale) const;

private:
  int order;

  std::list< Term > coefficients;
  //std::vector< double > coefficients;

  friend std::istream& operator>> (std::istream&, Polynomial&);
  friend std::ostream& operator<< (std::ostream&, const Polynomial&);

  // Internal utility function - computes and remembers the order
  // of a polynomial. Use this at the end of any function that can change
  // the coefficients of the polynomial.
  void checkOrder();

};

inline
Polynomial operator* (double scale, const Polynomial& p)
{ return p * scale; }

/* Note: a member function of the Polynomial class must have a polynomial
   as the first argument (e.g., poly.operator*(x) <===> poly * x). This
   function simply allows for the multiplication to be written with the
   polynomial on the right. */

#endif


/*
 * term.h
 *
 *  Created on: Jun 10, 2013
 *      Author: zeil
 */

#ifndef TERM_H_
#define TERM_H_

#include <iostream>

struct Term {
	double coefficient;
	int power;

	Term (double c = 0.0, int pow = 0)
	: coefficient(c), power(pow) {}

	bool operator== (const Term& t) const
	{
		return power == t.power && coefficient == t.coefficient;
	}
};

inline
std::ostream& operator<< (std::ostream& out, const Term& t) {
  out << t.coefficient;
  if (t.power > 0)
    {
      out << " x";
      if (t.power > 1)
	out << "^" << t.power;
      return out;
    }
}

#endif /* TERM_H_ */


// #include <iostream>

#include "polynomial.h"  // includes <iostream> //
//#include "polynomial.cpp" // to do a quick test... //



using namespace std;



int main( void ) // (int argc, char** argv) //
                 // NOT used here, compiler complains  //
				 // but may be needed for prof testing ??? //
{
  int nCoeff1;

  cout << "Enter a polynomial.\n";

  while( true )
  {
  	cout << "How many coefficients will be in the first polynomial? " << flush;
  	cin >> nCoeff1;
  	if( cin.good() && nCoeff1 > 0 ) break;
  	// else ...
  	cout << "\nPlease enter an array size > 0 \n";
  	cin.clear(); // clear all cin error flags ...
  	cin.sync(); // 'flush' cin stream ...
  }
  cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above

  double* coeff1 = new double[nCoeff1];

  cout << "\nEnter the " << nCoeff1 << " coefficients, in order of "
       << "increasing power of x:" << endl;
  for ( int i = 0 ; i < nCoeff1; ++i )
  {
  	cin >> coeff1[i];
  	if( !cin.good() )
  	{
  		cout << "\nInvalid input ... Please start over.\n"
			 << "Enter the " << nCoeff1 <<  " coefficients, in order "
			 << "of increasing power of x:" << endl;

  		cin.clear();
  		cin.sync();
  		i = -1; // ***  SET to -1 here so when incremented becomes 0 *** //
  	}
  }
  cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above

  Polynomial p1 ( nCoeff1, coeff1 ); // calls ( size, ary ) ctor...

  double x;
  while( true )
  {
    cout << "\nEnter a value at which to evaluate the polynomial: " << flush;
	cin >> x;
	if( cin.good() ) break;
	// else
	cout << "\nOnly decimal values are valid here...";
	cin.clear();
	cin.sync();
  }
  cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above

  cout << p1 << "\n  at x=" << x << " evaluates to " << p1.evaluate(x)
       << endl;

  Polynomial p2;	// calls default ctor ...
  while( true )
  {
    cout << "\nEnter a second polynomial, in the same format\n"
         << "(# of coefficients, then each coefficient in order "
	     << "of increasing power of x)." << endl;
	  cin >> p2;
	  if( cin.good() ) break;
	  else
	  {
	  	cin.clear();
	  	cin.sync();
	  }
  }
  cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above

  double scale;
  while( true )
  {
    cout << "\nEnter a scaling factor (a floating point number): " << flush;
    cin >> scale;
    if( cin.good() ) break;
    // else
    cin.clear();
    cin.sync();
    cout << "\nInvalid input ... ";
  }
  cin.sync(); // 'flush' any extra numbers (or char's) that might have been input above


  { // these extra {} not needed in C++ to declare new objects, but ok if wish //
    Polynomial p3 = p1 * scale;

    cout << "\n\n(" << p1 << ") * "
		 << scale
		 << "  = " << p3 << endl;
  }

  {
    Polynomial p4 = p1 + p2;

    cout << "\n\n" << p1 << "\n   +\n"
		 << " (" << p2 << ")\n"
		 << "  = " << p4 << endl;
  }

  {
    Polynomial p5 = p1 + scale * p2;

    cout << "\n\n" << p1 << "\n   +\n"
		 << scale << " * (" << p2 << ")\n"
		 << "  = " << p5 << endl;

    cout << "\nThis polynomial has order " << p5.getOrder()
		 << " and, at x = " << x << ", evaluates to " << p5.evaluate(x)
		 << endl;
  }

  delete [] coeff1;

  cout << "\nPress 'Enter' to continue/exit ... " << flush;
  cin.get();
  return 0;
}



ERRORS:
1. polynomial.cpp:66: error: no match for 'operator<' in 'a.std::_List_const_iterator<_Tp>::operator* [with _Tp = Term]() < b.std::_List_const_iterator<_Tp>::operator* [with _Tp = Term]()'

Would I have to overload the operator < as well? Would I have to delcar it in my polynomial.h as Polynomial operator< (const Polynomial& p)const;?

Any advice would be welcomed. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: no match for 'operator<'.....

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: no match for 'operator<'.....

Posted 16 June 2013 - 11:17 AM

We're not going to scrub through hundreds of lines of your code just because you didn't specify where the problem was.
Show us the line that produces the error.
Was This Post Helpful? 0
  • +
  • -

#3 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: no match for 'operator<'.....

Posted 16 June 2013 - 11:29 AM

View PosttlhIn`toq, on 16 June 2013 - 11:17 AM, said:

We're not going to scrub through hundreds of lines of your code just because you didn't specify where the problem was
Show us the line that produces the error.



You are mistaken. If you would kindly notice the section where I posted the error see: "Errors:"

>> We're not going to scrub throw hundreds. ...

The error message tells you the file. It also tells you the line....in this case the line is 66.

You'd be more than welcome to speak down on me if I was in the wrong. However, I was not. Please read and pay attention to what an OP provides.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: no match for 'operator<'.....

Posted 16 June 2013 - 11:41 AM

LIne 66 huh? Did you even name your blocks so we know which is which?

From first block you provided
066 {


Second block
66

Third block doesn't have 66 lines

Fourth block
066

None of these blocks have a line 66 that even contains an < operator.

What are we supposed to do with that?

But, as you said, I'm wrong. Sorry I have nothing to offer. I'll unsubscribe from the thread now. Best of luck with your issues.

This post has been edited by tlhIn`toq: 16 June 2013 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#5 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: no match for 'operator<'.....

Posted 16 June 2013 - 11:50 AM

It would appear I was too hasty to defend myself. For future viewers it is first block line 65 not 66.

1st - polynomial.cpp
2nd - polynomial.h
3rd - term.h
4th- polytest.cpp
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,432
  • Joined: 25-December 09

Re: no match for 'operator<'.....

Posted 16 June 2013 - 12:31 PM

Quote

Would I have to overload the operator < as well?

That would be one way.

Another way would be to compare one of the data members instead of trying to compare the entire object. Since your comment says your only comparing power something like:
  	if( a->power < (*b ).power ) // comparing powers only

Note: I used two different ways to reference the power, they are equivalent, use one or the other which ever is easier for you to understand.


Jim

This post has been edited by jimblumberg: 16 June 2013 - 12:31 PM

Was This Post Helpful? 1
  • +
  • -

#7 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: no match for 'operator<'.....

Posted 16 June 2013 - 12:37 PM

Thank you jim for the reply. I will try the first part of the if statement I prefer that look. I am out celebrating fathers day at the moment but Will reply with how things turned out by tonight.
Was This Post Helpful? 0
  • +
  • -

#8 mustardpower  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 02-December 12

Re: no match for 'operator<'.....

Posted 16 June 2013 - 12:38 PM

Shouldn't you use
if(a->getOrder()<b->getOrder())

This post has been edited by mustardpower: 16 June 2013 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: no match for 'operator<'.....

Posted 16 June 2013 - 05:16 PM

Thanks jim that took care of my error!
Was This Post Helpful? 0
  • +
  • -

#10 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 195
  • Joined: 19-September 12

Re: no match for 'operator<'.....

Posted 16 June 2013 - 05:56 PM

View Postmustardpower, on 16 June 2013 - 12:38 PM, said:

Shouldn't you use
if(a->getOrder()<b->getOrder())


I believe if the term.h file was placed into the Polynomial.h file under private in the class then yes. If I used that now the compiler will complain that term doesn't have any member named getOrder.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1