CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Posts
    2

    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.

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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
    Code:
    Date d;
    not
    Code:
    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...
    Old Unix programmers never die, they just mv to /dev/null

  3. #3
    Join Date
    Mar 2005
    Posts
    2

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured