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:

#include <iostream>
#include <ostream>

class Date
{
	int d, m, y;
public:
	Date();
	~Date();
	Date(int n);
	Date(const Date&);
	Date day(int);
	void print() const;
	bool operator== (const Date&);
	bool operator!= (const Date&);
	Date& operator+ (const Date&);
	ostream& operator<< (ostream&, Date&);
};


ostream& Date::operator<< (ostream& obj, Date& dd)
{
	obj << dd;
	return obj;
}

Date& Date::operator+ (const Date& obj)
{
	d += obj.d;
	m += obj.m;
	y += obj.y;
	return *this;
}

bool Date::operator!= (const Date& first)
{
	return first.d != d || first.m != m || first.y != y;
}
bool Date::operator== (const Date& first)
{
	return first.d == d && first.m == m && first.y == y;
}

Date::Date()
{
	d = 1;
	m = 1;
	y = 1;
}

Date::Date(int n):d(n), m(), y()
{
}

Date::Date(const Date& obj)
{
	d = obj.d;
	m = obj.m;
	y = obj.y;
}

void Date::print() const
{
	std::cout << d << " " << m << " " << y << std::endl;
}

Date Date::day(int n)
{
	d += n;
	return *this;
}

int main()
{
	Date o1;
	o1.print();
	o1.day(5);
	o1.print();
	Date o2(10);
	o2.print();
	Date o3(o1);
	o3.print();
	Date o4;
	o4 = o1 + o2;
	(o1 + o3).print();
	o4.print();
}

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

Recommended Answers

All 2 Replies

use ofstream instead of ostream. Then include <fstream> instead of <ostream>. Finally, add using statements after the include files

#include <fstream>
using std::ofstream;
using std::ifstream; // if you want this

Thank you !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.