Problem with functions...

This is a discussion on Problem with functions... within the C++ Programming forums, part of the General Programming Boards category; I'm trying to pass an ofstream object to another function. This is my attempt here (simplified to what matters): Code: ...

  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
    21,931
    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);
    }
    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

  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
    21,931
    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.
    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

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, 09: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, 09: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

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