I am writing a program that will implement an ADT for polynomials of the form: c0+c1x+c2x2+c3x3+… Each Polynomial object will save the appropriate coefficients for a single polynomial. I think that I have the code doing the right things, however I am getting the following undefined reference errors:
undefined reference to Polynomial::Polynomial(int, double*)
undefined reference to Polynomial::Polynomial(int, double*)
undefined reference to Polynomial::operator==(Polynomial const&) const
undefined reference to Polynomial::operator*(double) const
undefined reference to Polynomial::operator+(Polynomial const&) const' undefined reference to
Polynomial::operator+(Polynomial const&) const'
undefined reference to Polynomial::getDegree() const' undefined reference to
Polynomial::getDegree() const'
undefined reference to Polynomial::getCoeff(int) const' undefined reference to
Polynomial::getDegree() const'
undefined reference to Polynomial::operator*(double) const' undefined reference to
WinMain@16'
I have included my code and header file. I am confused as to the output of these errors as they are coming formt he output filr and the compiler does not give me any hints as to where I can look. Any help or pointers as to why/how this is happening will be greatly appreciated. Thank you!
Here is my code.
#include "polynomial.h"
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cassert>
using namespace std;
Polynomial::Polynomial ()
{
cout << "Enter a polynomial.\nHow many coefficients will be in the first polynomial? " << flush;
int nCoeff1;
cin >> nCoeff1;
double coeff1 [nCoeff1];
coefficients.push_back(nCoeff1);
for (int i = 0; i < nCoeff1; ++i)
cin >> coeff1[i];
cout << "\nEnter the coefficients, in order of increasing power of x:\n " << flush;
Polynomial p1 (nCoeff1, coeff1);
double x;
cout << "\nEnter a value at which to evaluate the polynomial: " << flush;
cin >> x;
cout << p1 << "\n at x=" << x << " evaluates to " << setprecision(3) << p1.evaluate(x)
<< endl;
cout << "\nEnter a second polynomial, in the same format\n(# of coefficients, then each coefficient in order of increasing power of x).\n " << flush;
Polynomial p2 (nCoeff1, coeff1);
coefficients.push_back(x);
cin >> p2;
if (p1 == p2)
{
cout << "Those polynomials are equal." << endl;
}
else
{
cout << "Those polynomials are not equal." << endl;
}
double scale;
cout << "\nEnter a scaling factor (a floating point number): " << flush;
cin >> scale;
{
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 degree " << p5.getDegree()
<< " and, at x=" << x << ", evaluates to " << setprecision(3) << p5.evaluate(x)
<< endl;
}
}
Polynomial::Polynomial (const vector<double>& coeff)
: coefficients(coeff)
{
normalize();
}
void Polynomial::normalize ()
{
while (coefficients.size() > 0 && coefficients.back() == 0.0)
coefficients.pop_back();
}
double Polynomial::evaluate (double x) const
{
double sum = 0.0;
int k = coefficients.size();
for (vector<double>::size_type i = 0; i < coefficients.size(); ++i)
{
--k;
sum = x * sum + coefficients[k];
}
return sum;
}
std::istream& operator>> (std::istream& in, Polynomial& p)
{
int r;
in >> r;
Polynomial result;
result.coefficients.resize(r);
for (int i = 0; i < r; ++i)
in >> result.coefficients[i];
result.normalize();
p = result;
return in;
}
std::ostream& operator<< (std::ostream& out, const Polynomial& p)
{
if (p.getDegree() < 0)
{
out << "0";
}
else
{
bool first_printed = true;
for (int i = 0; i <= p.getDegree(); ++i)
{
double c = p.getCoeff(i);
if (c != 0.0)
{
if (!first_printed)
{
out << " + ";
}
first_printed = false;
out << c;
if (i > 0)
{
out << " x";
if (i > 1)
{
out << "^" << i;
}
}
}
}
}
return out;
}
Here is my header file:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <vector>
/**
This class implements a polynomial of the form
c0 + c1 x + c2 x^2 + c3 x^3 + ...
(The order of a polynomial is the largest
power of x having a non-zero coefficient, or zero if the polynomial
has all zero coefficients.) Note that adding polynomials
together or scaling polynomials (multiplying by a constant) could
reduce the order of the result.
*/
class Polynomial {
public:
/**
* Create a zero polynomial.
*/
Polynomial ();
/**
* Create a polynomial with the given coefficients.
* E.g.,
* double c[3] = {1.0, 2.0, 3.0};
* Polynomial p (3, c);
* creates a polynomial p representing: 1.0 + 2.0*x + 3.0*x^2
*
* @param nCoeff number of coefficients in the input array
* @param coeff the coefficients of the new polynomial, starting
* with power 0.
*/
Polynomial (int nCoeff, double coeff[]);
/**
* Create a polynomial with the given coefficients. If
* the coeff vector is empty, create a zero polynomial.
*
* @param coeff the coefficients of the new polynomial, with
* coeff[0] denoting the term of power 0.
*/
Polynomial (const std::vector<double>& coeff);
// Don't need our own Big 3 because none of the data members are pointers
// Polynomial (const Polynomial&);
// ~Polynomial();
// Polynomial& operator= (const Polynomial&);
/**
* 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
*
* @param x the value on which this polynomial should be evaluated.
* @return the value of this polynomial at x
*/
double evaluate (double x) const;
/**
* Get the coefficient of the term with the indicated power.
* @param power the power of a term
* @return the corresponding coefficient, or zero if power < 0 or if
* power > getDegree()
*/
double getCoeff(int power) const;
/**
* The degree of a polynomial is the largest exponent of x with a
* non-zero coefficient. E.g.,
* x^3 + 2x + 1 has degree 3
* 42 has degree 0 (42 == 42 * x^0)
* 0 is a special case and is regarded as degree -1
*
* @return the degree of this polynomial
*/
int getDegree() const;
/**
* Add a polynomial to this one, returning their sum.
*
* @param p another polynomial
* @return the sum of the two polynomials
*/
Polynomial operator+ (const Polynomial& p) const;
/**
* Multiply this polynomial by a scalar.
* @param scale the value by which to multiply each term in this polynomial
* @return the polynomial resulting from this multiplication
*/
Polynomial operator* (double scale) const;
/**
* Compare this polynomial for equality against another.
*
* @param p another polynomial
* @return true iff the polynomials have the same degree and their corresponding
* coefficients are equal.
*/
bool operator== (const Polynomial& p) const;
private:
std::vector<double> coefficients;
friend std::istream& operator>> (std::istream&, Polynomial&);
//friend std::ostream& operator<< (std::ostream&, const Polynomial&);
/**
* A utility function to scan the current polynomial, putting it into
* "normal form". In this case, the normalized form should not have a
* zero coefficient for its highest power term. For example, the
* normal form of 0 x^3 + 3 x^2 + 0 x + 12 would be 3 x^2 + 0 x + 12.
*
* Polynomials should be kept in normal form at all times outside of the
* actual member function bodies of this class.
*/
void normalize();
};
/**
* Read a polynomial from an istream.
* Polynomials are input in the format:
* #coefficients c0 c1 ...
* where the ci are floating point coefficients of x^i
*
* @param in the stream from which to read the polynomial
* @param p the polynomial in which to store the result.
* @return the stream in
*/
std::istream& operator>> (std::istream& in, Polynomial& p);
/**
* Write a polynomial to an ostream
*
* @param out the stream to which to write the polynomial
* @param p the polynomial to write
* @return the stream out
*/
std::ostream& operator<< (std::ostream&, const Polynomial&);
/**
* 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.
*
* @param scale the value to multiply the polynomial by
* @param p a polynomial
* @return the product of scale and p
*/
inline
Polynomial operator* (double scale, const Polynomial& p)
{ return p * scale; }
#endif