C++: incomplete type and cannot be defined

A very confusing error just now:
error: aggregate ‘std::ofstream out’ has incomplete type and cannot be defined

from simple code:
std::ofstream out;

And the similar one:
error: variable ‘std::ofstream out’ has initialiser but incomplete type

from this code:
std::ofstream out(fname.c_str(),std::ios_base::app);

Using this didn’t help:
#include <iostream>

Using this also didn’t help:
#include <ofstream>

That was confusing me most, but now I see I’ve been getting “error: ofstream: No such file or directory” and I was missing it in the noise of other warnings and errors.

The solution was simple:
#include <fstream>
Yes, a very simple solution, but google wasn’t helping. If it had been saying “ofstream is not a member of std” I’d have known I was missing a header file; a strange error message has you looking in other places. (I guess another std header file is doing a forward declaration for ofstream, which is why we get “incomplete type”.)

One thought on “C++: incomplete type and cannot be defined

Leave a Reply

Your email address will not be published. Required fields are marked *