Ofstream... broke as a result of ifstream fix

This is a discussion on Ofstream... broke as a result of ifstream fix within the C++ Programming forums, part of the General Programming Boards category; Now I'm having troubles with writing the file out. I'M SO CLOSE to finishing this assignment! OK, here is my ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    Ofstream... broke as a result of ifstream fix

    Now I'm having troubles with writing the file out. I'M SO CLOSE to finishing this assignment!

    OK, here is my call to the function:

    Code:
    case 5: cout << "Enter the name of the File to save to:" << endl;
    				getline(cin, nameOfFile);
    				entry.writeToFile(nameOfFile);
    				break;
    Here is my function writeToFile():

    Code:
    void AddressBook::writeToFile(string fileName) 
    {
    	ofstream outfile;
    	
    	outfile.open(fileName.c_str());
    	if(!outfile)
    	{
    		cout <<"Cannot write to the file" << endl;
    	}
    	addressTree->writeFile();
    	outfile.close();  
    }//end writeToFile
    That function calls the following in my binary tree file:

    Code:
    template< typename NODETYPE >
    void Tree< NODETYPE >::writeFile() const
    {
    	ofstream out;
    	writeFileHelper(out, rootPtr);
    }//end function writeFile
    
    template< typename NODETYPE >
    void Tree< NODETYPE >::writeFileHelper(ofstream& out, TreeNode< NODETYPE > *ptr ) const
    {
    	ofstream outfile; 
       if ( ptr != 0 ) 
       {
          writeFileHelper(out, ptr->leftPtr ); // traverse left subtree  
          outfile << ptr->data; // process node                
          writeFileHelper(out, ptr->rightPtr); // traverse right subtree
       } // end if
    } // end function inOrderHelper
    //  function to perform deleteFromTree
    The writeToFile worked before I changed the ifstream, but now I've been messing with it trying to get it to work. I get this error message:

    :\program files\microsoft visual studio\myprojects\bst addressbook\tree.h(29) : error C2061: syntax error : identifier 'ofstream'
    c:\program files\microsoft visual studio\myprojects\bst addressbook\tree.h(31) : see reference to class template instantiation 'Tree<NODETYPE>' being compiled
    c:\program files\microsoft visual studio\myprojects\bst addressbook\tree.h(97) : error C2061: syntax error : identifier 'ofstream'
    Error executing cl.exe.


    any ideas?

    This is so great, that there are people who can help. I hope I can do it at some point!

    Thank you for helping.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Note sure what is the cause of the error, but have you tried fully qualifying ofstream? It makes sense to use std::ofstream instead of ofstream in a header file since you would want to avoid propagating a namespace collision from a header file.

    Incidentally, I thought that it makes more sense for writeFile() to take a std::ofstream& parameter, at least from what I understand of the code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    Maybe include ofstream ?
    Maybe write std::ofstream out;
    When does this file get opened ?

    > ofstream outfile;
    Now you have both out and outfile.
    Neither of them seem to be initialised.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    34
    Thank you thank you thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-06-2002, 07:03 PM
  2. problems with ifstream and ofstream!! help me please!!!
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2002, 12:51 PM
  3. ifstream, ofstream
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 02-04-2002, 12:20 PM
  4. Ofstream and Ifstream
    By unanimous in forum C++ Programming
    Replies: 1
    Last Post: 12-31-2001, 09:44 AM
  5. STL to: Cin, Cout, ifstream & ofstream
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21