Incomplete type is not allowed: stringstream

C++TypesStringstream

C++ Problem Overview


Why does this line give the error Error: incomplete type is not allowed?

stringstream ss;

C++ Solutions


Solution 1 - C++

#include <sstream> and use the fully qualified name i.e. std::stringstream ss;

Solution 2 - C++

Some of the system headers provide a forward declaration of std::stringstream without the definition. This makes it an 'incomplete type'. To fix that you need to include the definition, which is provided in the <sstream> header:

#include <sstream>

Solution 3 - C++

An incomplete type error is when the compiler encounters the use of an identifier that it knows is a type, for instance because it has seen a forward-declaration of it (e.g. class stringstream;), but it hasn't seen a full definition for it (class stringstream { ... };).

This could happen for a type that you haven't used in your own code but is only present through included header files -- when you've included header files that use the type, but not the header file where the type is defined. It's unusual for a header to not itself include all the headers it needs, but not impossible.

For things from the standard library, such as the stringstream class, use the language standard or other reference documentation for the class or the individual functions (e.g. Unix man pages, MSDN library, etc.) to figure out what you need to #include to use it and what namespace to find it in if any. You may need to search for pages where the class name appears (e.g. man -k stringstream).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpighead10View Question on Stackoverflow
Solution 1 - C++Prasoon SauravView Answer on Stackoverflow
Solution 2 - C++Yakov GalkaView Answer on Stackoverflow
Solution 3 - C++raksliceView Answer on Stackoverflow