943,526 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6810
  • C++ RSS
Oct 14th, 2008
0

undeclared identifier ostream

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fishky is offline Offline
7 posts
since Mar 2008
Oct 14th, 2008
0

Re: undeclared identifier ostream

use ofstream instead of ostream. Then include <fstream> instead of <ostream>. Finally, add using statements after the include files
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005
Oct 15th, 2008
0

Re: undeclared identifier ostream

Thank you !
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fishky is offline Offline
7 posts
since Mar 2008

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: Integer place values
Next Thread in C++ Forum Timeline: C++ help needed





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


Follow us on Twitter


© 2011 DaniWeb® LLC