undeclared identifier ostream

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 7
Reputation: fishky is an unknown quantity at this point 
Solved Threads: 0
fishky fishky is offline Offline
Newbie Poster

undeclared identifier ostream

 
0
  #1
Oct 14th, 2008
Hello guys, I'm selfstudying C++ and atm am reading about classes. I want to define ostream operator<< but compiler cant find ostream to be regular use. I'm using Visual Studio 2005 on XP SP2 and have MSDN installed. And another question - am I using proper initialisation for operator<< ? Thanks in advance for your help. Any suggestions on my programming style will be appreciated too.

Here's my code:

  1. #include <iostream>
  2. #include <ostream>
  3.  
  4. class Date
  5. {
  6. int d, m, y;
  7. public:
  8. Date();
  9. ~Date();
  10. Date(int n);
  11. Date(const Date&);
  12. Date day(int);
  13. void print() const;
  14. bool operator== (const Date&);
  15. bool operator!= (const Date&);
  16. Date& operator+ (const Date&);
  17. ostream& operator<< (ostream&, Date&);
  18. };
  19.  
  20.  
  21. ostream& Date::operator<< (ostream& obj, Date& dd)
  22. {
  23. obj << dd;
  24. return obj;
  25. }
  26.  
  27. Date& Date::operator+ (const Date& obj)
  28. {
  29. d += obj.d;
  30. m += obj.m;
  31. y += obj.y;
  32. return *this;
  33. }
  34.  
  35. bool Date::operator!= (const Date& first)
  36. {
  37. return first.d != d || first.m != m || first.y != y;
  38. }
  39. bool Date::operator== (const Date& first)
  40. {
  41. return first.d == d && first.m == m && first.y == y;
  42. }
  43.  
  44. Date::Date()
  45. {
  46. d = 1;
  47. m = 1;
  48. y = 1;
  49. }
  50.  
  51. Date::Date(int n):d(n), m(), y()
  52. {
  53. }
  54.  
  55. Date::Date(const Date& obj)
  56. {
  57. d = obj.d;
  58. m = obj.m;
  59. y = obj.y;
  60. }
  61.  
  62. void Date::print() const
  63. {
  64. std::cout << d << " " << m << " " << y << std::endl;
  65. }
  66.  
  67. Date Date::day(int n)
  68. {
  69. d += n;
  70. return *this;
  71. }
  72.  
  73. int main()
  74. {
  75. Date o1;
  76. o1.print();
  77. o1.day(5);
  78. o1.print();
  79. Date o2(10);
  80. o2.print();
  81. Date o3(o1);
  82. o3.print();
  83. Date o4;
  84. o4 = o1 + o2;
  85. (o1 + o3).print();
  86. o4.print();
  87. }

Error 1 error C2143: syntax error : missing ';' before '&' c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 17
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 17
Error 3 error C2061: syntax error : identifier 'ostream' c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 17
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 17
Error 5 error C2805: binary 'operator <<' has too few parameters c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 17
Error 6 error C2143: syntax error : missing ';' before '&' c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 21
Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 21
Error 8 error C2065: 'obj' : undeclared identifier c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 21
Error 9 error C2065: 'dd' : undeclared identifier c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 21
Error 10 error C2275: 'Date' : illegal use of this type as an expression c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 21
Error 11 error C2761: 'int &Date::operator <<(void)' : member function redeclaration not allowed c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 21
Error 12 fatal error C1903: unable to recover from previous error(s); stopping compilation c:\documents and settings\pesho\my documents\visual studio 2005\projects\klasove\klasove\this.cpp 21
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: undeclared identifier ostream

 
0
  #2
Oct 14th, 2008
use ofstream instead of ostream. Then include <fstream> instead of <ostream>. Finally, add using statements after the include files
  1. #include <fstream>
  2. using std::ofstream;
  3. using std::ifstream; // if you want this
Last edited by Ancient Dragon; Oct 14th, 2008 at 8:45 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: fishky is an unknown quantity at this point 
Solved Threads: 0
fishky fishky is offline Offline
Newbie Poster

Re: undeclared identifier ostream

 
0
  #3
Oct 15th, 2008
Thank you !
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC