Sunday, June 6, 2010

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".)

Mumble, grumble, back to work.

7 comments:

AJ said...

yup, you were right! same thing happened to me. thanks! all it needed was the fstream.

Bobby said...

I had a slightly different problem, but it was just a missing inclusion. It's great to have an idea where to look. Thanks!

Tony said...

Thank you !! you saved me much time searching for such a trick.
Thank you one more time :)

Michail said...

Thank you. You saved my day. ;)

Lizzie said...

Same problem here. Thanks a lot!

CW said...

Thank you! I was just having the same problem. You saved me quite a bit of time.

Anonymous said...

Thanks