C++ variable has initializer but incomplete type?

C++ClassCompiler Construction

C++ Problem Overview


I am trying to compile 2 classes in C++ with the following command:

g++ Cat.cpp Cat_main.cpp -o Cat

But I receive the following error:

Cat.cpp:10:10: error: variable ‘Cat Joey’ has initializer but incomplete type

Could someone explain to me what this means? What my files basically do is create a class (Cat.cpp) and create an instance (Cat_main.cpp). Here is my source code:

Cat.cpp:

#include <iostream>
#include <string>

class Cat;

using namespace std;

int main()
{
	Cat Joey("Joey");
	Joey.Meow();

	return 0;
}

Cat_main.cpp:

#include <iostream>
#include <string>

using namespace std;

class Cat
{
	public:
		Cat(string str);
	// Variables
		string name;
	// Functions
		void Meow();
};

Cat::Cat(string str)
{
	this->name = str;
}

void Cat::Meow()
{
	cout << "Meow!" << endl;
	return;
}

C++ Solutions


Solution 1 - C++

You use a forward declaration when you need a complete type.

You must have a full definition of the class in order to use it.

The usual way to go about this is:

  1. create a file Cat_main.h

  2. move

    #include

    class Cat { public: Cat(std::string str); // Variables std::string name; // Functions void Meow(); };

to Cat_main.h. Note that inside the header I removed using namespace std; and qualified string with std::string.

  1. include this file in both Cat_main.cpp and Cat.cpp:

    #include "Cat_main.h"

Solution 2 - C++

It's not related to Ken's case directly, but such an error also can occur if you copied .h file and forgot to change #ifndef directive. In this case compiler will just skip definition of the class thinking that it's a duplication.

Solution 3 - C++

Sometimes, the same error occurs when you forget to include the corresponding header.

Solution 4 - C++

You cannot define a variable of an incomplete type. You need to bring the whole definition of Cat into scope before you can create the local variable in main. I recommend that you move the definition of the type Cat to a header and include it from the translation unit that has main.

Solution 5 - C++

I got a similar error and hit this page while searching the solution.

With Qt this error can happen if you forget to add the QT_WRAP_CPP( ... ) step in your build to run meta object compiler (moc). Including the Qt header is not sufficient.

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
Questionuser569322View Question on Stackoverflow
Solution 1 - C++Luchian GrigoreView Answer on Stackoverflow
Solution 2 - C++Dmitriy SukharevView Answer on Stackoverflow
Solution 3 - C++xtluoView Answer on Stackoverflow
Solution 4 - C++David Rodríguez - dribeasView Answer on Stackoverflow
Solution 5 - C++peter karasevView Answer on Stackoverflow