In C++ Language, header files are very important. If we are using some keywords or built-in methods of C++, we must have to add their respective header files in our code otherwise the compiler will give us the error. Now, to understand the error better, first, we will discuss what header files are and what is their purpose. Header files are included at the top of the code.
Header files are just like import libraries, which we import, to use their functionalities. These files sometimes act as class files that have different functions in them. Header files are necessary to add for using those. If we try to use those functions directly and do not import the header files, we get this error. This error also occurs when the compiler detects any unknown identifier that has a known datatype but does not have a known definition. Sometimes this error also occurs if you are using an outdated IDE or compiler.
This error occurs in different types which are as follows:
- incomplete type is not allowed
- stringstream incomplete type is not allowed
- ifstream incomplete type is not allowed
Syntax:
This is not a built-in function or library. We are rather discussing that it is an error and as we all know that an error does not has a specific syntax. This is the message that the compiler returns when we get our error.
error: field ‘Parent’ has incomplete type ‘A’
Note that in the above error message, the keywords ‘Parent’ and ‘A’ are not static. They keep changing depending on your code. This thing will be understood after we perform the examples.
Example # 01:
To have a better understanding of our error, we will now perform an example to get the error. After that, we will resolve the error. To do so, we have declared a class by the name of “TestClass”. In our class in the public access modifier section, we have created an object of our class by the name of “Parent”.
After that, we created a pointer of our class which is pointing towards the pointer of the object of our class. We are not doing anything else nor we are calling anything in the main() method or performing any operation. This is the simplest example we have tried to perform to make you understand. Now, we’ll run our code to see the results.
using namespace std;
class TestClass
{
public:
TestClass Parent; /*To resolve error:TestClass *Parent;*/
TestClass(TestClass *ptr) : Parent(*ptr)
{
}
};
int main ()
{
}
On compilation of our code, the system has thrown the error and has not successfully compiled it. To discover the reason for this error, let’s look at the error message. The error message says “field ‘Parent’ has incomplete type ‘TestClass’ “. This is the same error we are discussing that is an incomplete type that is not allowed. So, the system has shown this error because we have an incomplete type which we know by now is not allowed. Now, we will resolve our error.
To resolve our error, we have not done anything extra. We have just replaced the object of TestClass from a simple object to a pointer. We’ll now run our modified code to observe how it works.
This time, our code has successfully executed without giving any error. We did not perform any operation in our code, so it has a blank output. The cause of the error was that we were trying to create an instance of the object of a class which is not possible because we have used it in the constructor already. In that case, the compiler calls the code recursively and it becomes infinite that is why it is not possible.
Example # 02:
We already discussed in our introduction part that this error can be caused due to multiple reasons. Our objective here is to discuss as many reasons as possible. So, to achieve our objective, we have performed another simple example to make you understand the error. To perform this example, we have not created any class, structure or function, or anything. We simply have called stringstream which is used to operate with strings such as calculating the number of words etc.
using namespace std;
int main()
{
std::stringstream obj;
return 0;
}
This is the error we will get after the execution of our code which means that this time, also, the code was not able to compile successfully. The error message says that aggregate std::stringstream “obj” has an incomplete type and cannot be defined. So, here, the error message is slightly different from the previous one. But the point here is the same: that it is an “incomplete type”. But why did this problem happen this time when we didn’t utilize a class? We will try to eliminate the error and by doing so we will find out the reason for its occurrence also.
We discussed earlier that this error also occurs if the header file is missing in our code. So, we will add a header file that is ‘sstream’ and see what happens. Let’s execute our code.
This time, the code is successfully executed. The reason for the error was that we were using the stringstream function, but we were not adding its library in the header files section. After adding its header file, the code has executed successfully.
Conclusion
In this guide, we discussed an error that occurs during coding in C++ Language: that error is ‘Incomplete type’. We discussed the cause of the error and the ways to eliminate this error. We also performed examples handling different scenarios to make you understand the error. At last, we will summarize the whole article by saying that you have to make sure of a few things to avoid getting such errors. Those few things are that we must check whether we have included all the required header files in our code or not. Check that the datatypes of the objects are correct or not. In most cases, we are using simple objects, but our code requires to use of pointers. If we are doing a forward declaration of any structure or class in our code, we have to check whether the definition of that class or structure is available in our code or not.