Thread: Ofstream... broke as a result of ifstream fix

  1. #1
    Registered User
    Join Date
    Nov 2006

    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:

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

    void AddressBook::writeToFile(string fileName) 
    	ofstream outfile;
    		cout <<"Cannot write to the file" << endl;
    }//end writeToFile
    That function calls the following in my binary tree file:

    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  4. #4
    Registered User
    Join Date
    Nov 2006
    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