hi!
I have a problem about "polynomial class ".when I Insert the firend function "operator<<" in the program.it doesn't run and have 5 error about
missing ";" in private member :"_degree" and missing ")" in first line(prototype)
of friend function "operator<<".
why this program have this ERRRS???!!! :(
can you help me??thanx for repling!!!

class poly {
  friend ostream& operator<<(ostream&,const poly&);
  //friend poly operator-(const poly&,const poly&);
  //friend poly operator*(const poly&,const poly&);
  //friend poly operator+(const poly&,const poly&);
  //friend poly operator-(const poly&);
  //friend list merge(iterator b1,iterator e1,iterator b2,iterator e2);
  private:
  long int _degree;
  //void reduce();
  public:
  poly(double=0,unsigned=0);
  double operator()(double) const;
  long degreep() const;
  unsigned termsp() const;
  //static const poly ZERO; //p(x)=0
  //static const poly ONE;  //p(x)=1
  //static const poly X;    //p(x)=x
  list _terms;      //WARNING:IT IS TEMPORARY!!!
 };
//------------------------------------------------------------------------------
poly::poly(double coef,unsigned exp){

		if(coef==0.0){
			_terms=list(0);
			_degree= -1;
		  }
		else
		  {_terms=list(1,term(coef,exp));
			_degree=exp;
		  }
	 }
long max(long a,long b){
	  return (a>b?a:b);
	 }
//------------------------------------------------------------------------------
list merge(iterator b1,iterator e1,iterator b2,iterator e2){
  list destlist;
  while((b1!=e1)&&(b2!=e2))
		 {if((*b1)<(*b2))
			 {destlist.push_back(*b1);
				b1++;
			 }
		  else
			 {destlist.push_back(*b2);
			  b2++;
			 }
		 }
  while(b1!=e1)
		  {destlist.push_back(*b1);
			b1++;
		  }
  while(b2!=e2)
		  {destlist.push_back(*b2);
			b2++;
		  }
  return destlist;
}
//------------------------------------------------------------------------------

ostream& operator<<(ostream &ostr,const poly &p){

	//if(p==poly::ZERO)  return ostr<<0;
//	iterator itr=p._terms.begin();
//	ostr<<*itr++;
//	while(itr!=p._terms.end())
//		  if (((*itr)._coef)<0) ostr<<"-"<<abs(*itr++);
//		  else              ostr<<"+"<<*itr++;
//	return ostr;

}
//------------------------------------------------------------------------------

//poly operator+(const poly& p1,const poly& p2){
//	  poly p;
//	  p._degree=max(p1._degree,p2._degree);
//	  p._terms=list(p1._terms.size()+p2._terms.size());
//	  merge(p1._terms.begin(),p1._terms.end(),p2._terms.begin(),p2._terms.end());
//	  p.reduce();
//	  return p;
//}
//------------------------------------------------------------------------------
//poly operator*(const poly &p1,const poly &p2){
//	  poly p;
//	  p._degree=p1._degree+p2._degree;
//	  for(iterator it1=p._terms.begin();it1!=p.terms._end;it1++)
//			for(iterator it2=p._terms.begin();it2!=p.terms._end;it2++){
//				 double coef=(*it1)._coef * (*it2)._coef;
//				 unsigned exp=(*it2)._coef * (*it2)._coef;
//				 iterator it=p._terms.begin();
//				 for( ;it!=p._terms.end();it++)
//					  if ((*it)._exp <=exp) break;
//				 if((*it)._exp == exp)
//					 (*it)._coef +=coef;
//				 else
//					 p._terms.insert(it,term(coef,exp));
//				}
//	  p.reduce();
//	  return p;
//  }
//------------------------------------------------------------------------------
//double poly::operator()(double x) const{
//
//		 iterator it=_terms.begin();
//		 if(it==_terms.end()) return 0;
//
//		 unsigned e1=(*it)._exp;
//		 double y=(*it)._coef;
//		 while(++it!=_terms.end())
//				{unsigned e2=(*it)._exp;
//				 y*=pow(x,(e1-e2));
//				 y+=(*it)._coef;
//				 e1=e2;
//				}
//		 return y*pow(x,e1);

//	}

Well, since your code is so incomplete there's no telling what the error is. Are you including the proper headers? Are you accounting for the std namespace? Is everything declared before it's used?

Well, since your code is so incomplete there's no telling what the error is. Are you including the proper headers? Are you accounting for the std namespace? Is everything declared before it's used?

OK! It is complete source code of my "polynomial class"!!
can you give your idea? :confused: what is the problem of this program?
before writing the "operator+" friend function it properly run and operator<<
works!!! :rolleyes:
you can compile this code to see the errors. (the function reduce() doesn't writen yet!!)
tnx.

#include <iostream.h>
#include <math.h>
//|----------|
//|class term|
//|----------|
  class term
	{
	 friend ostream& operator<<(ostream&,const term&);
	 friend term operator-(const term&);
	 friend term abs(const term&);
	 public:
	 term(double=0.0,unsigned=0);
	 int operator==(const term&) const;
	 int operator!=(const term&) const;
	 int operator<(const term&) const;
	 int operator>(const term&) const;
	 int operator<=(const term&) const;
	 double _coef;
	 unsigned _exp;
	};

//------------------------------------------------------------------------------
ostream& operator<<(ostream& ostr,const term& term1){

	 if(term1._exp==0)
		 return  ostr<<term1._coef;
	 if(term1._coef==1)
		 ostr<<"x";
	 else if(term1._coef==-1)
		 ostr<<"-x";
	 else
		 ostr<<term1._coef<<"x";
	 if(term1._exp==1)
		 return ostr;
	 else
		 return ostr<<"^"<<term1._exp;
	}
//------------------------------------------------------------------------------
term  operator-(const term& term1){
  term t(term1);
  t._coef *=-1.0;
  return t;
 }
//------------------------------------------------------------------------------
term  abs(const term& t){
	term term1(t);
	if (term1._coef<0)  term1._coef*=-1.0;
	return term1;
  }
//------------------------------------------------------------------------------
term::term(double coef,unsigned exp):_coef(coef),_exp(exp){}
//-------------------------------------------------------------------------------
int term::operator==(const term& t) const{
	 return _exp==t._exp && _coef==t._coef;
	}

int term::operator!=(const term& t) const{
	 return _exp!=t._exp || _coef!=t._coef;
	}

int term::operator<(const term& t) const{
	 return _exp<t._exp;          //is it correct??
	}
int term::operator>(const term& t) const{
	 return _exp>t._exp;          //is it correct??
	}

int term::operator<=(const term& t) const{
	 return !(_exp>t._exp);          //is it correct??
	}
//******************************************************************************
//|----------|
//|class node|
//|----------|
class node{                       //!!

	 public:
	 node(const term& data=term(),node *prev=0,node *next=0)
	 :_data(data),_prev(prev),_next(next)
	 {if(_prev==0)  _prev=this;
	  if(_next==0)  _next=this;
	 }
	 term _data;
	 node *_prev,*_next;
	};
//******************************************************************************
//|--------------|
//|class iterator|
//|--------------|
  class iterator{
	  friend class list;
	  public:
	  iterator(node *p):_(p){}
	  term& operator*(){return _->_data;}
	  void operator=(const iterator& it){_=it._;}     //is it correct?
	  int operator==(const iterator& it){return _==it._;}
	  int operator!=(const iterator& it){return _!=it._;}
	  iterator operator++(int)    //postfix
	  {
		iterator it(_);
		_=_->_next;
		return it;
	  }
	  iterator& operator++(){_=_->_next; return *this;}
	  iterator operator--(int)
	  {
		iterator it(_);
		_=_->_prev;
		return it;
	  }
	  iterator& operator--(){_=_->_prev; return *this;}
	  //protected:
	  node *_;
	};
//******************************************************************************
//|----------|
//|class list|
//|----------|
class list{

  protected:

  node *_;                       //!!
  int _size;                     //!!`

  public:

  list();
  list(const list&);
  list(int);
  list(int,const term&);
  list(iterator&,iterator&);
  ~list();
  int size() const;
  int isempty() const;
  iterator begin() const;
  iterator end() const;
  void push_back(const term&);
  iterator insert(iterator&,const term&);
};
//------------------------------------------------------------------------------
list::list(){_=new node();}
//------------------------------------------------------------------------------


list::list(const list& l):_size(l._size){
	  _=new node();
	  node *pp=_;
	  for(node *p=l._->_next;p!=l._;p=p->_next , pp=pp->_next)
			pp->_next=pp->_next->_prev=new node(p->_data,_,pp);
	  }
//------------------------------------------------------------------------------

list::list(int n):_size(n){
	 _=new node();
	 node *p=_;
	 for(int i=0;i<n;i++)
		  p=p->_prev=new node(term(),_,p);
	 _->_next=p;
  }
//------------------------------------------------------------------------------
list::list(int n,const term& t){
	 _=new node();
	 node *p=_;
	 for(int i=0;i<n;i++)
		  p=p->_prev=new node(t,_,p);
	 _->_next=p;
  }

 //-----------------------------------------------------------------------------


list::~list(){
	 node *p=_->_next;
	 while(p!=_)
			{node *pp=p->_next;
			 delete p;
			 p=pp;
			}
	 delete _;
 }
//------------------------------------------------------------------------------

int list::size() const{return _size;}
//------------------------------------------------------------------------------

int list::isempty() const { return _size==0;}
//------------------------------------------------------------------------------
iterator  list::begin() const{return iterator(_->_next);}
//------------------------------------------------------------------------------

iterator  list::end() const {return iterator(_);}
//------------------------------------------------------------------------------

void list::push_back(const term& x){
	_->_prev=_->_prev->_next=new node(x,_->_prev,_);
	++_size;
  }
//------------------------------------------------------------------------------
iterator list::insert(iterator &it,const term &t){
			it._->_prev=it._->_prev->_next=new node(t,it._->_prev,it._);
			it._=it._->_prev;
			++ _size;
			return it;
}
//******************************************************************************
//|----------|
//|class poly|
//|----------|

class poly {
  friend ostream& operator<<(ostream&,const poly&);
  //friend poly operator-(const poly&,const poly&);
  //friend poly operator*(const poly&,const poly&);
  //friend poly operator+(const poly&,const poly&);
  //friend poly operator-(const poly&);
  //friend list merge(iterator b1,iterator e1,iterator b2,iterator e2);
  private:
  long int _degree;
  //void reduce();
  public:
  poly(double=0,unsigned=0);
  double operator()(double) const;
  long degreep() const;
  unsigned termsp() const;
  //static const poly ZERO; //p(x)=0
  //static const poly ONE;  //p(x)=1
  //static const poly X;    //p(x)=x
  list _terms;      //WARNING:IT IS TEMPORARY!!!
 };
//------------------------------------------------------------------------------
poly::poly(double coef,unsigned exp){

		if(coef==0.0){
			_terms=list(0);
			_degree= -1;
		  }
		else
		  {_terms=list(1,term(coef,exp));
			_degree=exp;
		  }
	 }
//------------------------------------------------------------------------------
//const poly poly::ZERO(0,0);
//const poly poly::ONE(1.0,0);
//const poly poly::X(1.0,1);

//------------------------------------------------------------------------------

long max(long a,long b){
	  return (a>b?a:b);
	 }
//------------------------------------------------------------------------------
list merge(iterator b1,iterator e1,iterator b2,iterator e2){
  list destlist;
  while((b1!=e1)&&(b2!=e2))
		 {if((*b1)<(*b2))
			 {destlist.push_back(*b1);
				b1++;
			 }
		  else
			 {destlist.push_back(*b2);
			  b2++;
			 }
		 }
  while(b1!=e1)
		  {destlist.push_back(*b1);
			b1++;
		  }
  while(b2!=e2)
		  {destlist.push_back(*b2);
			b2++;
		  }
  return destlist;
}
//------------------------------------------------------------------------------

ostream& operator<<(ostream &ostr,const poly &p){

	//if(p==poly::ZERO)  return ostr<<0;
//	iterator itr=p._terms.begin();
//	ostr<<*itr++;
//	while(itr!=p._terms.end())
//		  if (((*itr)._coef)<0) ostr<<"-"<<abs(*itr++);
//		  else              ostr<<"+"<<*itr++;
//	return ostr;

}
//------------------------------------------------------------------------------

//poly operator+(const poly& p1,const poly& p2){
//	  poly p;
//	  p._degree=max(p1._degree,p2._degree);
//	  p._terms=list(p1._terms.size()+p2._terms.size());
//	  merge(p1._terms.begin(),p1._terms.end(),p2._terms.begin(),p2._terms.end());
//	  p.reduce();
//	  return p;
//}
//------------------------------------------------------------------------------
//poly operator*(const poly &p1,const poly &p2){
//	  poly p;
//	  p._degree=p1._degree+p2._degree;
//	  for(iterator it1=p._terms.begin();it1!=p.terms._end;it1++)
//			for(iterator it2=p._terms.begin();it2!=p.terms._end;it2++){
//				 double coef=(*it1)._coef * (*it2)._coef;
//				 unsigned exp=(*it2)._coef * (*it2)._coef;
//				 iterator it=p._terms.begin();
//				 for( ;it!=p._terms.end();it++)
//					  if ((*it)._exp <=exp) break;
//				 if((*it)._exp == exp)
//					 (*it)._coef +=coef;
//				 else
//					 p._terms.insert(it,term(coef,exp));
//				}
//	  p.reduce();
//	  return p;
//  }
//------------------------------------------------------------------------------
//double poly::operator()(double x) const{
//
//		 iterator it=_terms.begin();
//		 if(it==_terms.end()) return 0;
//
//		 unsigned e1=(*it)._exp;
//		 double y=(*it)._coef;
//		 while(++it!=_terms.end())
//				{unsigned e2=(*it)._exp;
//				 y*=pow(x,(e1-e2));
//				 y+=(*it)._coef;
//				 e1=e2;
//				}
//		 return y*pow(x,e1);

//	}
//------------------------------------------------------------------------------
  void main(){
  }

Well, it compiles fine for me with minor unrelated changes. I would guess that because you're using a pre-standard header (iostream.h), it's not doing something that it should. Change this:

#include <iostream.h>
#include <math.h>

To this:

#include <iostream>
#include <cmath>

using namespace std;

And see what happens. If that doesn't work, specify your compiler and operating system.

well, I tried to install "Microsoft Visual Studio Enterprise Edition " in windows xp(because I found that borland turbo c++ 4.5!!! can not include this headers) but setup failed because of following error:

"setup was unable to create a DCOM user accont in order to register D:\programfiles\microsoft visual studio\common\tools\vs_ent98\vanalyzer\valec.exe"

**DCOM=>acronym of Distributed Object Model**
and in windows ME this error apeared:

object FD 5164: RegMso object failed to load Mso97.dll

what is the problem?? :-|
before formatting my computer I have visual c++ on my system but now it doesn't install!!


Well, it compiles fine for me with minor unrelated changes. I would guess that because you're using a pre-standard header (iostream.h), it's not doing something that it should. Change this:

#include <iostream.h>
#include <math.h>

To this:

#include <iostream>
#include <cmath>

using namespace std;

And see what happens. If that doesn't work, specify your compiler and operating system.

Well, it compiles fine for me with minor unrelated changes. I would guess that because you're using a pre-standard header (iostream.h), it's not doing something that it should. Change this:

#include <iostream.h>
#include <math.h>

To this:

#include <iostream>
#include <cmath>

using namespace std;

And see what happens. If that doesn't work, specify your compiler and operating system.

I found that I have borland c++ 5.2 compiler.it supports namespaces(but no namespace std) and new headers(whitout .h(but no <cmath>)).my operating system is windows xp.

>**DCOM=>acronym of Distributed Object Model**
Thanks for telling me that. I wouldn't have known otherwise. :rolleyes:

>what is the problem??
I have no idea, try searching google groups for your error message.

>I found that I have borland c++ 5.2 compiler.
Download Borland C++ 5.5, it's free. Or download Dev-C++, it's also free. Or download Visual C++ 2005 Express, it's free too! They all support the current C++ standard very well. You'll find that you have fewer problems with the language itself when you're not restrained by your implementation.

This article has been dead for over six months. Start a new discussion instead.