Input and Output of User-Defined Types
Hello all. I have what should be a simple problem but I just can't get it to compile. I'm using Dev-C++ 4 on Windows 98. Basically I am creating a Date object and I want to be able to output this object with a line such as:
Code:
Date d();
std::cout << "Date object: " << d << std::endl;
The file Date.h looks like this:
Code:
#ifndef Date_h
#define Date_h
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class Date
{
public:
// Constructors
private:
// Variables
// output inserter
template<class charT, class Traits>
friend basic_ostream<charT,Traits>&
operator<<(basic_ostream<charT,Traits>& os, const Date& d);
};
#endif // Date_h
When I try to compile this I get the following errors:
Code:
Date.h:31: ANSI C++ forbids declaration `basic_ostream' with no type
Date.h:31: template-id `basic_ostream<charT, Traits>' used as a declarator
Date.h:31: `basic_ostream' is neither a function nor method; cannot be declared friend
Date.h:31: invalid member template declaration `basic_ostream'
Date.h:31: parse error before `&'
Line 31 is the one that reads
Code:
friend basic_ostream<charT,Traits>&
Any help would be greatly appreciated.
Re: Input and Output of User-Defined Types
I also use Dev-Cpp and the code compiles fine with the exception that in your code, you're constructing the variable d incorrectly:
Code:
Date d();
std::cout << "Date object: " << d << std::endl;
It should be not , because Date d() means you're declaring a function called d that returns type Date, and that's not exactly what you want to do... :rolleyes:
Re: Input and Output of User-Defined Types
Thanks for telling me that. I uninstalled Dev-Cpp and installed a newer version and it worked just fine.