C++ 错误:有初始化程序但类型不完整

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4287428/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:02:43  来源:igfitidea点击:

Error: has initializer but incomplete type

c++

提问by olidev

I am having the problem: has initializer but incomplete type while using struct:

我遇到了问题:使用结构时有初始化程序但类型不完整:

in a hpp file:

在 hpp 文件中:

class A
{
private:
   struct videoDT;
};

in a cpp file:

在 cpp 文件中:

struct A::videoDT
{
  videoDT(int b) : a(b){}

  int a;
};

void test()
{
   struct videoDT test(1);
}

Then I have the problem:

然后我有问题:

Error: has initializer but incomplete type

错误:有初始化程序但类型不完整

Thanks in advance

提前致谢

采纳答案by sbi

I think the problem is that test()doesn't have access to A's private types.

我认为问题在于test()无法访问A的私有类型。

This compiles for me:

这为我编译:

class A
{
private:
    friend void test();
    struct videoDT;
};

struct A::videoDT
{
    videoDT(int b) : a(b){}

    int a;
};

void test()
{
    A::videoDT test(1);
}

回答by AnT

In your testfunction you declare a local type struct videoDT, but never define it. Not surpisingly, the compiler complains about an object of incomplete type being initialized. End of story.

在您的test函数中,您声明了一个本地类型struct videoDT,但从未定义它。毫不奇怪,编译器抱怨初始化了不完整类型的对象。故事结局。

How did you expect it to work? If you wanted your declaration to use A::videoDTtype, then you should have used the qualified name for the type - A::videoDT- since that's what that type is called. However, the code will not compile anyway, since A::videoDTis private in Aand testhas no access to it.

你期望它如何工作?如果您希望您的声明使用A::videoDT类型,那么您应该使用类型的限定名称 - A::videoDT- 因为这就是该类型的名称。但是,代码无论如何都不会编译,因为它A::videoDT是私有的A并且test无法访问它。

In other words, it is hard to figure out what you were trying to do. Provide some explanations or code that make more sense.

换句话说,很难弄清楚你想要做什么。提供一些更有意义的解释或代码。

回答by Jon

When the compiler processes the .hpp file, it needs to infer the memory footprint and layout of class A. For this reason, it needs to know the memory layout of struct videoDT.

编译器在处理.hpp文件时,需要推断类A的内存占用和布局,为此需要知道struct videoDT的内存布局。

The error is the compiler complaining that it doesn't know what struct videoDTis, since you define it in the .cpp file.

错误是编译器抱怨它不知道是什么struct videoDT,因为您在 .cpp 文件中定义了它。

To solve the error, you need to define the structin a .hpp file that gets included before your original .hpp. Alternatively, you can use the pimpl idiom and change class Ato:

要解决该错误,您需要struct在原始 .hpp 之前包含的 .hpp 文件中定义。或者,您可以使用 pimpl 成语并更改class A为:

class A
{
private:
    struct videoDT* pVideoDT;
}

If you do this, then you no longer need to define the struct in a header file just to compile class A. However, the pimpl idiom is an advanced technique and I suggest reading up on it before deciding to use it.

如果你这样做,那么你不再需要在头文件中定义结构体来编译 A 类。然而,pimpl idiom 是一种高级技术,我建议在决定使用它之前阅读它。