Click to See Complete Forum and Search --> : Input and Output of User-Defined Types


JasonOwens
March 12th, 2005, 01:45 PM
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:
Date d();
std::cout << "Date object: " << d << std::endl;
The file Date.h looks like this:
#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:
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 friend basic_ostream<charT,Traits>&
Any help would be greatly appreciated.

HighCommander4
March 12th, 2005, 02:52 PM
I also use Dev-Cpp and the code compiles fine with the exception that in your code, you're constructing the variable d incorrectly:

Date d();
std::cout << "Date object: " << d << std::endl;

It should be Date d; not Date d(), 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:

JasonOwens
March 12th, 2005, 04:03 PM
Thanks for telling me that. I uninstalled Dev-Cpp and installed a newer version and it worked just fine.