Thread: using ostream in a template

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    22

    using ostream in a template

    I have this class and have, among other things, a client print function. I have an assignment to make a client print function, however we weren't really told how to use ostream in a class, and what I'm doing doesn't seem to be right. This wasn't covered in our textbook.

    Anyway, this is what I have for the function.

    Code:
    template <class ItemType>
    void SortedType<ItemType>::PrintList(ostream& dataFile)
    {
    	Nodetype* location = listData;
    
    	if(location = NULL)
    		return;
    	do
    	{
    		dataFile<<location->data<<endl;
    		location = location->next;
    	}	
    	while(location->next != NULL)
    }
    Basically, it just traverses a sorted linked-list and prints all the data. But I get this error:

    "error C2061: syntax error : identifier 'ostream'"

    I included iostream, but it doesn't seem to do anything. Where am I going wrong? Thanks,

    -Tristan

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Could it be that you need to use std::ostream ?
    Last edited by laserlight; 12-02-2003 at 12:15 AM.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    RE:

    YES£¬maybe you should "using namespace std;"

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >maybe you should "using namespace std;"
    Not in any realistic program. Around here for brevity, sure. Otherwise it is better to explicitly resolve names in the std namespace with std::<name>, or use a using declaration:
    Code:
    using std::ostream;
    The latter is preferred for applications, the former for tools and libraries.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    hmmm if I do either of those VC++ tells me "'Nodetype' : undeclared identifier" and gives me loads of other errors regarding my structure not being there. That doesn't make any sense to me.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    hmmm it looks like it is a VC++ bug...I've found this on google, but did not read the whole thing yet.
    Last edited by axon; 12-02-2003 at 12:56 AM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    That link doesn't work.

    I supose it's worth mentioning that i'm using Visual Studio 6 right now... It might be fixed in newer versions I guess?

  8. #8
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    sorry, I fixed the link...it should work fine now....if it still doesn't for some odd reason this is the string I used to search for it: "using ostream AND c++"

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    Hmmmm... Okay, I found the bit about friend functions. I had that problem before actually as well on a different project, but fixed it with the latest service pack. Maybe this is just a similar bug that they didn't catch...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM