C++ 变量有初始值设定项但类型不完整?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9770980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ variable has initializer but incomplete type?
提问by
I am trying to compile 2 classes in C++ with the following command:
我正在尝试使用以下命令在 C++ 中编译 2 个类:
g++ Cat.cpp Cat_main.cpp -o Cat
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
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
) 并创建一个实例 ( Cat_main.cpp
)。这是我的源代码:
Cat.cpp:
猫.cpp:
#include <iostream>
#include <string>
class Cat;
using namespace std;
int main()
{
Cat Joey("Joey");
Joey.Meow();
return 0;
}
Cat_main.cpp:
猫_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;
}
采纳答案by Luchian Grigore
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
1)创建文件 Cat_main.h
2) move
2)移动
#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
.
到Cat_main.h
。请注意,在标题中,我删除了using namespace std;
字符串并使用std::string
.
3) include this file in both Cat_main.cpp
and Cat.cpp
:
3) 在Cat_main.cpp
和中都包含此文件Cat.cpp
:
#include "Cat_main.h"
回答by Dmitriy Sukharev
It's not related to Ken's case directly, but such an error also can occur if you copied .hfile and forgot to change #ifndef
directive. In this case compiler will just skip definition of the class thinking that it's a duplication.
它与 Ken 的案例没有直接关系,但是如果您复制了.h文件并且忘记更改#ifndef
指令,也会发生这样的错误。在这种情况下,编译器将跳过类的定义,认为它是重复的。
回答by David Rodríguez - dribeas
You cannot define a variable of an incomplete type. You need to bring the whole definition of Cat
into scope beforeyou 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
.
您不能定义不完整类型的变量。您需要将 的整个定义Cat
引入范围,然后才能在main
. 我建议您将类型的定义移动Cat
到标题中,并将其从具有main
.
回答by xtluo
Sometimes, the same error occurs when you forget to include the corresponding header
.
有时,当您使用forget to include the corresponding header
.
回答by peter karasev
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.
对于 Qt,如果您忘记QT_WRAP_CPP( ... )
在构建中添加步骤以运行元对象编译器 (moc),则可能会发生此错误。包括 Qt 头文件是不够的。