在头文件"unknown type"中使用结构是指:错误 [英] Using a struct in a header file "unknown type" error
问题描述
我正在Kubuntu中使用Kdevelop. 我已经在我的datasetup.h文件中声明了一个结构:
I am using Kdevelop in Kubuntu. I have declared a structure in my datasetup.h file:
#ifndef A_H
#define A_H
struct georeg_val {
int p;
double h;
double hfov;
double vfov;
};
#endif
现在当我在main.c文件中使用它时
Now when I use it in my main.c file
int main()
{
georeg_val gval;
read_data(gval); //this is in a .cpp file
}
我收到以下错误:
georeg_chain.c:7:3:错误:未知类型名称'georeg_val'
georeg_chain.c:7:3: error: unknown type name 'georeg_val'
(在georeg_val gval;
行中)
如果有人能帮助我解决此错误,我将不胜感激.
I would appreciate if anyone could help me resolve this error.
推荐答案
在C语言中,有两种方法可以声明结构:
In C one has two possibilities to declare structure:
struct STRUCT_NAME {} ;
或
typedef struct {} STRUCT_ALIAS;
如果使用第一种方法(为结构命名),则必须通过将变量明确标记为a struct
来定义变量:
If you use first method (give struct a name) - you must define variable by marking it explicitly being a struct
:
struct STRUCT_NAME myStruct;
但是,如果您使用第二种方法(给别名构造一个别名),则可以省略struct
标识符-编译器可以推断出仅属于alias
的变量类型:
However if you use second method (give struct an alias) then you can omit struct
identifier - compiler can deduce type of variable given only it's alias
:
STRUCT_ALIAS myStruct;
奖励积分: 您可以使用其名称和别名声明struct:
Bonus points: You can declare struct with both it's name and alias:
typedef struct STRUCT_TAG {} STRUCT_TAG;
// here STRUCT_NAME == STRUCT_ALIAS
然后在变量定义中,您可以使用第一种或第二种方法.为什么两个世界都好?使用结构别名可以使结构变量定义更短-有时候这是一件好事.但是结构名称让您创建forward declarations
.在某些情况下,这是必不可少的工具-考虑到您在结构之间具有循环引用:
Then in variable definition you can use either first or second method. Why both of two worlds is good ? Struct alias lets you to make struct variable definitions shorter - which is a good thing sometimes. But struct name let's you to make forward declarations
. Which is indispensable tool in some cases - consider you have circular references between structs:
struct A {
struct B * b;
}
struct B {
struct A * a;
}
除了该体系结构可能存在缺陷之外-当以第一种方式(使用名称)声明结构并通过将结构指针标记为struct
显式引用结构指针时,将编译此循环定义.
Besides that this architecture may be flawed - this circular definition will compile when structs are declared in the first way (with names) AND struct pointers are referenced explicitly by marking them as struct
.
这篇关于在头文件"unknown type"中使用结构是指:错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!