passing ofstream into a structure

How do i pass an ofstream to a structure;
1
2
3
4
5
6
7
8
9
10
11
//write to file
ofstream outfile[4];

outfile[0].open("example1.bin");
outfile[1].open("example2.bin");
outfile[2].open("example3.bin");

//structure
structure poll[4];

poll[index].writer = outfile[index];

its complaining that there is no appropriate default constructor available.
1
2
3
4
5
6
7
8
struct structure 
{
    HANDLE              hTerminate;
    HANDLE              hBarrier;
    HANDLE              hCom;
    ofstream		&outFile;
    std::vector<DWORD>  *timestamps;
};
Change the structure def to:
1
2
3
4
5
6
7
8
struct structure 
{
    HANDLE              hTerminate;
    HANDLE              hBarrier;
    HANDLE              hCom;
    ofstream		*outFile;
    std::vector<DWORD>  *timestamps;
};

And then change line 11 to:
poll[index].outFile = &(outfile[index]);

I notice there is no placeholder in structure that is called "writer".
Is it complaining on line 2 about no default constructor?
The std::ostream class only has one explicit constructor and it is not the default.
http://www.cplusplus.com/reference/ostream/ostream/ostream/


Edit: I just noticed std::ofstream was the topic and not std::ostream
Last edited on
Did you really intend for timestamps to be a pointer to a vector?
Why not just allocate it directly?
 
std::vector<DWORD>  timestamps;


its complaining that there is no appropriate default constructor available.
Well, that's because str::ofstream doesn't have a default constructor, as Kevin said.

You've defined structure.outFile as a reference to a std::ofstream, which means you need to initialise it with an actual std::ofstream object.

You'll need to write a constructor for the struct, which takes an ofstream object (or, even better, a reference to an ofstream object) as an argument. In the initialisation list for that constructor, you can initialise outFile with that argument.

Remember, in C++, a struct is exactly the same as a class, except that members are public by default. This means that you can write constructors (and other methods) for them exactly as if they were classes.

Alternatively, you could do what Kevin suggested: make structure.outFile a pointer, and assign a valid value to it after you've instantiated the struct.

Edit: Apologies for the misinformation in the first line of my response.
Last edited on
thanks for the replies.
I tried it this way and it works.
1
2
3
4
5
6
7
8
9
10
11
structure poll[number_ports] = {(outfile[0]),(outfile[1]),(outfile[2]),(outfile[3])};

struct structure{
structure(ofstream &obj) : fileout(obj){};	
  
    HANDLE                        hTerminate;
    HANDLE                        hBarrier;
    HANDLE                        hCom;
    ofstream			&fileout;
    std::vector<DWORD>  *timestamps;
};
You're welcome :)
> How do i pass an ofstream to a structure;
> its complaining that there is no appropriate default constructor available.

>> I tried it this way and it works.

If you haven't realized it already, spend a few minutes now in understanding what the problem really was.

It had nothing to do with a std::ofstream as a non-static member per se.
It had everything to do with a non-static member being a reference, and that a reference has no default initialization.

You would have got precisely the same error with
1
2
3
4
5
6
7
8
9
struct A
{
    int& reference ;
};

int main()
{
    A a ;
}

And the fix would have been precisely the same: a constructor that initializes the reference.

Btw, std::ofstream is default constructible.
Btw, std::ofstream is default constructible.

Oops, yes, you're right. Thanks for catching that! Apologies for the misinformation I gave in my post.
Topic archived. No new replies allowed.