Thread: Problem with functions...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Problem with functions...

    I'm trying to pass an ofstream object to another function. This is my attempt here (simplified to what matters):

    Code:
    #include <fstream>
    
    inline void FunctionTwo(ofstream Bfile)
    {
    	bfile<<"test";
    }
    
    
    main()
    {
    	ofstream Afile("test.txt", ios::trunc);
    	functiontwo(Afile);
    }
    I have also tried pointers but they failed for me too (I'm not 100% comfortable with pointers yet). So how would I go about doing this?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    it doesnt seem immediately obvious to me as to the error: what is the error/problem?

    a small thing to note, is that your function declares the variable Bfile, yet you use <<operator on the variable bfile, but i imagine that isnt your error.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is probably that you need to pass the ofstream by reference, but you tried to pass it by value.

    Aside from that your example code suffers from typos etc. Try:
    Code:
    #include <fstream>
    
    inline void FunctionTwo(std::ofstream& Bfile)
    {
        bfile << "test";
    }
    
    int main()
    {
        std::ofstream Afile("test.txt", std::ios::trunc);
        FunctionTwo(Afile);
    }
    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

  4. #4
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    I fixed the problem, I changed it to this:

    Code:
    inline void FunctionTwo(ofstream* Bfile)
    {
    	*bfile<<"test";
    }
    
    
    main()
    {
    	ofstream Afile("test.txt", ios::trunc);
    	functiontwo(&Afile);
    }
    Thanks for your help.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I fixed the problem
    Passing a pointer works too, but here passing by reference is more typical, and really, what you want to do.

    Also, main() returns an int, so say as much.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with functions
    By saliya in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 10:36 PM
  2. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  3. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM
  4. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  5. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM