Hey,
I was compiling an old programming assignment about manipulating polynomials represented as linked list of its terms and I ran into a plethora of unfamiliar errors. I originally used Visual Studio Express to do the assignment, where it successfully compiled and ran, if I remember correctly. Recently, however, I installed Ubuntu on an old Thinkpad and decided to give gcc/g++ a test run. Unfortunately, it failed to compile and gave me a whole host of errors in the header file where I define my term and polynomial classes. I didn't get these errors in VS and don't see why I'd be getting them now. Here are the errors and relevant code.
In file included from assign7.cpp:9:
Poly.h:20: error: expected ‘,’ or ‘...’ before ‘&’ token
Poly.h:20: error: ISO C++ forbids declaration of ‘Poly’ with no type
Poly.h:21: error: expected ‘,’ or ‘...’ before ‘&’ token
Poly.h:21: error: ISO C++ forbids declaration of ‘Poly’ with no type
Poly.h: In function ‘std::ostream& operator<<(std::ostream&, const Poly&)’:
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:286: error: within this context
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:291: error: within this context
Poly.h:32: error: ‘int Term::degree’ is private
Poly.h:295: error: within this context
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:296: error: within this context
Poly.h:32: error: ‘int Term::degree’ is private
Poly.h:297: error: within this context
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:299: error: within this context
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:302: error: within this context
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:306: error: within this context
Poly.h:32: error: ‘int Term::degree’ is private
Poly.h:307: error: within this context
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:309: error: within this context
Poly.h:32: error: ‘int Term::degree’ is private
Poly.h:309: error: within this context
Poly.h:33: error: ‘Term* Term::next_term’ is private
Poly.h:313: error: within this context
Poly.h:31: error: ‘float Term::coefficient’ is private
Poly.h:316: error: within this context
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
/*
[Removed personal info]
*/
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
/* A class representing a term of a polynomial as a node in a linked list. Contains value of coefficient,
degree, and a pointer to the next list */
class Term
{
friend class Poly;
friend ostream& operator<<(ostream& out, const Poly& p);
friend istream& operator>>(istream& in, const Poly& p);
public:
Term(float c, int d) { coefficient = c; degree = d; next_term = 0; }
Term() { coefficient = 0; degree = 0; next_term = 0; }
const float evaluate(float x) const { return ( coefficient * pow(x,degree) ); }
const Term operator+(const Term& b) const;
const Term operator*(const Term& b) const;
const bool operator==(const Term& b) const;
const bool operator!=(const Term& b) const;
private:
float coefficient;
int degree;
Term *next_term;
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
ostream& operator<<(ostream& out, const Poly& p)
{
Term *current, *temp;
current = p.first_term;
if((current->coefficient) < 0)
out << "-";
while(current != 0)
{
if(current->coefficient == 0)
out << "0";
else
{
if(current->degree == 0)
out << fabs(current->coefficient);
else if(current->degree == 1)
{
if(fabs(current->coefficient) == 1)
out << "x";
else
out << fabs(current->coefficient) << "x";
}
else
{
if(fabs(current->coefficient) == 1)
out << "x^" << current->degree;
else
out << fabs(current->coefficient) << "x^" << current->degree;
}
}
temp = current->next_term;
if(temp != 0)
{
if((temp->coefficient) < 0)
out << " - ";
else
out << " + ";
}
current = temp;
}
return out;
}
|