'struct std::ostream' has no member named 'close' error in header file

Hi, LeafyCircuits here!

Comp specs:
OS: Windows 7 Premuim 64-bit
Compiler: GNU gcc with C++ 2011, ISO and STL
IDE: Dev C++ v4.9.9.2

NOTE: Code shown below is a "cut out" from the containing file, meaning it is a portion of code out of the entire file. This is done to save you time. Just keep in mind that there is more code to the file than the code shown, and expect me to know that it already compiles fine. If you want to see the entire file, let me know.

I've recently made a header file containing programming patterns that I developed for myself, essentially, common functions, classes, etc. that I use throughout most of my programs. More recently, I've been adding to the file some functions, and when I try to implement it into a project, the compiler is spewing errors again. The following is that header file. I've commented the lines that cause the error for your convenience.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef CORE_H
#define CORE_H

#include <iostream>
#include <fstream>
#include <sstream>
#include <conio.h>

using namespace std;

namespace file_ops
{
          template <typename T>
          void writeFile(string filename, T item, ios_base::fmtflags flag)      //I'll explain this in a sec
          {
               if (checkFile_exist(filename, nodisp)==true)
               {
                                                   ostream  outfile(filename.c_str(), ios_base::app | flag);
                                                   outfile << item;
                                                   outfile.close();        //Error line
               }
               else if (checkFile_exist(filename, nodisp)==false)
               {
                   ostream outfile(filename.c_str(), flag);
                   outfile << item;
                   outfile.close();           //Error line
               }
          }
}

#endif 


Specifically, the compiler's error for both error lines was:
'struct std::ostream' has no member named 'close'


Now, I know that 'ostream' does have the function close(), so my syntax is correct. What I worry to be the cause of the error is in the function header:
ios_base::fmtflags flag

What I want to be able to do is for one of the args in 'writeFile()' to be one of the flags from ios_base::fmtflags. I did a little research about this, and I came up with this approach to achieving my goal, and since it compiles fine, I assumed it to work, though, I am not sure.


Thanks for your time in reading and helping me with this problem; it is greatly appreciated! (•‿•)




I'm new to the forums, so constructive criticism on anything is also greatly appreciated!
ostreams do not have a close method:
http://www.cplusplus.com/reference/ostream/ostream/

you want to use an ofstream.
You also do not need to explicitly close a file anyway, because the dtor will do it for you automatically.
Plus if your set on closing your file, you should do it outside of your if /else options, that way when the function completes, it just closes the file, and your not having to do the work for each choice. Just have it as the last thing you do
Whoops, I was just using the wrong thing:
LowestOne said:
you want to use an ofstream.


This is what I wanted to use, thanks!
Topic archived. No new replies allowed.