944,168 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2750
  • C++ RSS
Jan 2nd, 2005
0

an "(" expected??

Expand Post »
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!!!
C++ Syntax (Toggle Plain Text)
  1. class poly {
  2. friend ostream& operator<<(ostream&,const poly&);
  3. //friend poly operator-(const poly&,const poly&);
  4. //friend poly operator*(const poly&,const poly&);
  5. //friend poly operator+(const poly&,const poly&);
  6. //friend poly operator-(const poly&);
  7. //friend list merge(iterator b1,iterator e1,iterator b2,iterator e2);
  8. private:
  9. long int _degree;
  10. //void reduce();
  11. public:
  12. poly(double=0,unsigned=0);
  13. double operator()(double) const;
  14. long degreep() const;
  15. unsigned termsp() const;
  16. //static const poly ZERO; //p(x)=0
  17. //static const poly ONE; //p(x)=1
  18. //static const poly X; //p(x)=x
  19. list _terms; //WARNING:IT IS TEMPORARY!!!
  20. };
  21. //------------------------------------------------------------------------------
  22. poly::poly(double coef,unsigned exp){
  23.  
  24. if(coef==0.0){
  25. _terms=list(0);
  26. _degree= -1;
  27. }
  28. else
  29. {_terms=list(1,term(coef,exp));
  30. _degree=exp;
  31. }
  32. }
  33. long max(long a,long b){
  34. return (a>b?a:b);
  35. }
  36. //------------------------------------------------------------------------------
  37. list merge(iterator b1,iterator e1,iterator b2,iterator e2){
  38. list destlist;
  39. while((b1!=e1)&&(b2!=e2))
  40. {if((*b1)<(*b2))
  41. {destlist.push_back(*b1);
  42. b1++;
  43. }
  44. else
  45. {destlist.push_back(*b2);
  46. b2++;
  47. }
  48. }
  49. while(b1!=e1)
  50. {destlist.push_back(*b1);
  51. b1++;
  52. }
  53. while(b2!=e2)
  54. {destlist.push_back(*b2);
  55. b2++;
  56. }
  57. return destlist;
  58. }
  59. //------------------------------------------------------------------------------
  60.  
  61. ostream& operator<<(ostream &ostr,const poly &p){
  62.  
  63. //if(p==poly::ZERO) return ostr<<0;
  64. // iterator itr=p._terms.begin();
  65. // ostr<<*itr++;
  66. // while(itr!=p._terms.end())
  67. // if (((*itr)._coef)<0) ostr<<"-"<<abs(*itr++);
  68. // else ostr<<"+"<<*itr++;
  69. // return ostr;
  70.  
  71. }
  72. //------------------------------------------------------------------------------
  73.  
  74. //poly operator+(const poly& p1,const poly& p2){
  75. // poly p;
  76. // p._degree=max(p1._degree,p2._degree);
  77. // p._terms=list(p1._terms.size()+p2._terms.size());
  78. // merge(p1._terms.begin(),p1._terms.end(),p2._terms.begin(),p2._terms.end());
  79. // p.reduce();
  80. // return p;
  81. //}
  82. //------------------------------------------------------------------------------
  83. //poly operator*(const poly &p1,const poly &p2){
  84. // poly p;
  85. // p._degree=p1._degree+p2._degree;
  86. // for(iterator it1=p._terms.begin();it1!=p.terms._end;it1++)
  87. // for(iterator it2=p._terms.begin();it2!=p.terms._end;it2++){
  88. // double coef=(*it1)._coef * (*it2)._coef;
  89. // unsigned exp=(*it2)._coef * (*it2)._coef;
  90. // iterator it=p._terms.begin();
  91. // for( ;it!=p._terms.end();it++)
  92. // if ((*it)._exp <=exp) break;
  93. // if((*it)._exp == exp)
  94. // (*it)._coef +=coef;
  95. // else
  96. // p._terms.insert(it,term(coef,exp));
  97. // }
  98. // p.reduce();
  99. // return p;
  100. // }
  101. //------------------------------------------------------------------------------
  102. //double poly::operator()(double x) const{
  103. //
  104. // iterator it=_terms.begin();
  105. // if(it==_terms.end()) return 0;
  106. //
  107. // unsigned e1=(*it)._exp;
  108. // double y=(*it)._coef;
  109. // while(++it!=_terms.end())
  110. // {unsigned e2=(*it)._exp;
  111. // y*=pow(x,(e1-e2));
  112. // y+=(*it)._coef;
  113. // e1=e2;
  114. // }
  115. // return y*pow(x,e1);
  116.  
  117. // }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
compeat is offline Offline
12 posts
since Jan 2005
Jan 2nd, 2005
0

Re: an "(" expected??

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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 3rd, 2005
0

Re: an "(" expected??

Quote originally posted by Narue ...
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? 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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <math.h>
  3. //|----------|
  4. //|class term|
  5. //|----------|
  6. class term
  7. {
  8. friend ostream& operator<<(ostream&,const term&);
  9. friend term operator-(const term&);
  10. friend term abs(const term&);
  11. public:
  12. term(double=0.0,unsigned=0);
  13. int operator==(const term&) const;
  14. int operator!=(const term&) const;
  15. int operator<(const term&) const;
  16. int operator>(const term&) const;
  17. int operator<=(const term&) const;
  18. double _coef;
  19. unsigned _exp;
  20. };
  21.  
  22. //------------------------------------------------------------------------------
  23. ostream& operator<<(ostream& ostr,const term& term1){
  24.  
  25. if(term1._exp==0)
  26. return ostr<<term1._coef;
  27. if(term1._coef==1)
  28. ostr<<"x";
  29. else if(term1._coef==-1)
  30. ostr<<"-x";
  31. else
  32. ostr<<term1._coef<<"x";
  33. if(term1._exp==1)
  34. return ostr;
  35. else
  36. return ostr<<"^"<<term1._exp;
  37. }
  38. //------------------------------------------------------------------------------
  39. term operator-(const term& term1){
  40. term t(term1);
  41. t._coef *=-1.0;
  42. return t;
  43. }
  44. //------------------------------------------------------------------------------
  45. term abs(const term& t){
  46. term term1(t);
  47. if (term1._coef<0) term1._coef*=-1.0;
  48. return term1;
  49. }
  50. //------------------------------------------------------------------------------
  51. term::term(double coef,unsigned exp):_coef(coef),_exp(exp){}
  52. //-------------------------------------------------------------------------------
  53. int term::operator==(const term& t) const{
  54. return _exp==t._exp && _coef==t._coef;
  55. }
  56.  
  57. int term::operator!=(const term& t) const{
  58. return _exp!=t._exp || _coef!=t._coef;
  59. }
  60.  
  61. int term::operator<(const term& t) const{
  62. return _exp<t._exp; //is it correct??
  63. }
  64. int term::operator>(const term& t) const{
  65. return _exp>t._exp; //is it correct??
  66. }
  67.  
  68. int term::operator<=(const term& t) const{
  69. return !(_exp>t._exp); //is it correct??
  70. }
  71. //******************************************************************************
  72. //|----------|
  73. //|class node|
  74. //|----------|
  75. class node{ //!!
  76.  
  77. public:
  78. node(const term& data=term(),node *prev=0,node *next=0)
  79. :_data(data),_prev(prev),_next(next)
  80. {if(_prev==0) _prev=this;
  81. if(_next==0) _next=this;
  82. }
  83. term _data;
  84. node *_prev,*_next;
  85. };
  86. //******************************************************************************
  87. //|--------------|
  88. //|class iterator|
  89. //|--------------|
  90. class iterator{
  91. friend class list;
  92. public:
  93. iterator(node *p):_(p){}
  94. term& operator*(){return _->_data;}
  95. void operator=(const iterator& it){_=it._;} //is it correct?
  96. int operator==(const iterator& it){return _==it._;}
  97. int operator!=(const iterator& it){return _!=it._;}
  98. iterator operator++(int) //postfix
  99. {
  100. iterator it(_);
  101. _=_->_next;
  102. return it;
  103. }
  104. iterator& operator++(){_=_->_next; return *this;}
  105. iterator operator--(int)
  106. {
  107. iterator it(_);
  108. _=_->_prev;
  109. return it;
  110. }
  111. iterator& operator--(){_=_->_prev; return *this;}
  112. //protected:
  113. node *_;
  114. };
  115. //******************************************************************************
  116. //|----------|
  117. //|class list|
  118. //|----------|
  119. class list{
  120.  
  121. protected:
  122.  
  123. node *_; //!!
  124. int _size; //!!`
  125.  
  126. public:
  127.  
  128. list();
  129. list(const list&);
  130. list(int);
  131. list(int,const term&);
  132. list(iterator&,iterator&);
  133. ~list();
  134. int size() const;
  135. int isempty() const;
  136. iterator begin() const;
  137. iterator end() const;
  138. void push_back(const term&);
  139. iterator insert(iterator&,const term&);
  140. };
  141. //------------------------------------------------------------------------------
  142. list::list(){_=new node();}
  143. //------------------------------------------------------------------------------
  144.  
  145.  
  146. list::list(const list& l):_size(l._size){
  147. _=new node();
  148. node *pp=_;
  149. for(node *p=l._->_next;p!=l._;p=p->_next , pp=pp->_next)
  150. pp->_next=pp->_next->_prev=new node(p->_data,_,pp);
  151. }
  152. //------------------------------------------------------------------------------
  153.  
  154. list::list(int n):_size(n){
  155. _=new node();
  156. node *p=_;
  157. for(int i=0;i<n;i++)
  158. p=p->_prev=new node(term(),_,p);
  159. _->_next=p;
  160. }
  161. //------------------------------------------------------------------------------
  162. list::list(int n,const term& t){
  163. _=new node();
  164. node *p=_;
  165. for(int i=0;i<n;i++)
  166. p=p->_prev=new node(t,_,p);
  167. _->_next=p;
  168. }
  169.  
  170. //-----------------------------------------------------------------------------
  171.  
  172.  
  173. list::~list(){
  174. node *p=_->_next;
  175. while(p!=_)
  176. {node *pp=p->_next;
  177. delete p;
  178. p=pp;
  179. }
  180. delete _;
  181. }
  182. //------------------------------------------------------------------------------
  183.  
  184. int list::size() const{return _size;}
  185. //------------------------------------------------------------------------------
  186.  
  187. int list::isempty() const { return _size==0;}
  188. //------------------------------------------------------------------------------
  189. iterator list::begin() const{return iterator(_->_next);}
  190. //------------------------------------------------------------------------------
  191.  
  192. iterator list::end() const {return iterator(_);}
  193. //------------------------------------------------------------------------------
  194.  
  195. void list::push_back(const term& x){
  196. _->_prev=_->_prev->_next=new node(x,_->_prev,_);
  197. ++_size;
  198. }
  199. //------------------------------------------------------------------------------
  200. iterator list::insert(iterator &it,const term &t){
  201. it._->_prev=it._->_prev->_next=new node(t,it._->_prev,it._);
  202. it._=it._->_prev;
  203. ++ _size;
  204. return it;
  205. }
  206. //******************************************************************************
  207. //|----------|
  208. //|class poly|
  209. //|----------|
  210.  
  211. class poly {
  212. friend ostream& operator<<(ostream&,const poly&);
  213. //friend poly operator-(const poly&,const poly&);
  214. //friend poly operator*(const poly&,const poly&);
  215. //friend poly operator+(const poly&,const poly&);
  216. //friend poly operator-(const poly&);
  217. //friend list merge(iterator b1,iterator e1,iterator b2,iterator e2);
  218. private:
  219. long int _degree;
  220. //void reduce();
  221. public:
  222. poly(double=0,unsigned=0);
  223. double operator()(double) const;
  224. long degreep() const;
  225. unsigned termsp() const;
  226. //static const poly ZERO; //p(x)=0
  227. //static const poly ONE; //p(x)=1
  228. //static const poly X; //p(x)=x
  229. list _terms; //WARNING:IT IS TEMPORARY!!!
  230. };
  231. //------------------------------------------------------------------------------
  232. poly::poly(double coef,unsigned exp){
  233.  
  234. if(coef==0.0){
  235. _terms=list(0);
  236. _degree= -1;
  237. }
  238. else
  239. {_terms=list(1,term(coef,exp));
  240. _degree=exp;
  241. }
  242. }
  243. //------------------------------------------------------------------------------
  244. //const poly poly::ZERO(0,0);
  245. //const poly poly::ONE(1.0,0);
  246. //const poly poly::X(1.0,1);
  247.  
  248. //------------------------------------------------------------------------------
  249.  
  250. long max(long a,long b){
  251. return (a>b?a:b);
  252. }
  253. //------------------------------------------------------------------------------
  254. list merge(iterator b1,iterator e1,iterator b2,iterator e2){
  255. list destlist;
  256. while((b1!=e1)&&(b2!=e2))
  257. {if((*b1)<(*b2))
  258. {destlist.push_back(*b1);
  259. b1++;
  260. }
  261. else
  262. {destlist.push_back(*b2);
  263. b2++;
  264. }
  265. }
  266. while(b1!=e1)
  267. {destlist.push_back(*b1);
  268. b1++;
  269. }
  270. while(b2!=e2)
  271. {destlist.push_back(*b2);
  272. b2++;
  273. }
  274. return destlist;
  275. }
  276. //------------------------------------------------------------------------------
  277.  
  278. ostream& operator<<(ostream &ostr,const poly &p){
  279.  
  280. //if(p==poly::ZERO) return ostr<<0;
  281. // iterator itr=p._terms.begin();
  282. // ostr<<*itr++;
  283. // while(itr!=p._terms.end())
  284. // if (((*itr)._coef)<0) ostr<<"-"<<abs(*itr++);
  285. // else ostr<<"+"<<*itr++;
  286. // return ostr;
  287.  
  288. }
  289. //------------------------------------------------------------------------------
  290.  
  291. //poly operator+(const poly& p1,const poly& p2){
  292. // poly p;
  293. // p._degree=max(p1._degree,p2._degree);
  294. // p._terms=list(p1._terms.size()+p2._terms.size());
  295. // merge(p1._terms.begin(),p1._terms.end(),p2._terms.begin(),p2._terms.end());
  296. // p.reduce();
  297. // return p;
  298. //}
  299. //------------------------------------------------------------------------------
  300. //poly operator*(const poly &p1,const poly &p2){
  301. // poly p;
  302. // p._degree=p1._degree+p2._degree;
  303. // for(iterator it1=p._terms.begin();it1!=p.terms._end;it1++)
  304. // for(iterator it2=p._terms.begin();it2!=p.terms._end;it2++){
  305. // double coef=(*it1)._coef * (*it2)._coef;
  306. // unsigned exp=(*it2)._coef * (*it2)._coef;
  307. // iterator it=p._terms.begin();
  308. // for( ;it!=p._terms.end();it++)
  309. // if ((*it)._exp <=exp) break;
  310. // if((*it)._exp == exp)
  311. // (*it)._coef +=coef;
  312. // else
  313. // p._terms.insert(it,term(coef,exp));
  314. // }
  315. // p.reduce();
  316. // return p;
  317. // }
  318. //------------------------------------------------------------------------------
  319. //double poly::operator()(double x) const{
  320. //
  321. // iterator it=_terms.begin();
  322. // if(it==_terms.end()) return 0;
  323. //
  324. // unsigned e1=(*it)._exp;
  325. // double y=(*it)._coef;
  326. // while(++it!=_terms.end())
  327. // {unsigned e2=(*it)._exp;
  328. // y*=pow(x,(e1-e2));
  329. // y+=(*it)._coef;
  330. // e1=e2;
  331. // }
  332. // return y*pow(x,e1);
  333.  
  334. // }
  335. //------------------------------------------------------------------------------
  336. void main(){
  337. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
compeat is offline Offline
12 posts
since Jan 2005
Jan 4th, 2005
0

Re: an "(" expected??

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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <math.h>
To this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
And see what happens. If that doesn't work, specify your compiler and operating system.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 7th, 2005
0

Re: an "(" expected??

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:
Quote ...
"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:
Quote ...
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!!



Quote originally posted by Narue ...
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <math.h>
To this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
And see what happens. If that doesn't work, specify your compiler and operating system.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
compeat is offline Offline
12 posts
since Jan 2005
Jan 8th, 2005
0

Re: an "(" expected??

Quote originally posted by Narue ...
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <math.h>
To this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
compeat is offline Offline
12 posts
since Jan 2005
Jan 8th, 2005
0

Re: an "(" expected??

>**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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help
Next Thread in C++ Forum Timeline: clipboard in console





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC