ChatGPT解决这个技术问题 Extra ChatGPT

C++ variable has initializer but incomplete type?

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;
}
You cannot declare objects for the classes whose definition are not yet visible.
@Ken: You have to include the header file.

a
atasca10

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 <string>

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.

3) include this file in both Cat_main.cpp and Cat.cpp:

#include "Cat_main.h"

So I can't throw my class definition into a different file?
@Ken no, that's not what I said. Read the answer again.
Would I define the functions for the class in the header file too?
@Ken no necessarily, you can leave the method definitions in the cpp file.
@Ken it's simple but it makes a big difference. You should read an introductory book to C++ where these concepts are explained in detail, there's much more to it than this.
J
John_West

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.


Excellent comment, I think it covers the most frequent cause of the problem. This is because copying or renaming header file and forgetting about #ifndef directive is tricky while other answers addressed forgetting to add #include statement which is a trivial mistake.
I have the same problem in my code, what does it mean to change the #ifndef directive. The directive already exists in all the header files.
x
xtluo

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


D
David Rodríguez - dribeas

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.


So I have to put my Cat class in the file with main?
@Ken: no, you have to read an introduction book about C++ where the concept of headers and sources files will be explained.
@Ken: Create a header file with the definition of the class (and the appropriate guards), then include it both from cat.cpp and cat_main.cpp.
J
John_West

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.