解引用指向不完整类型的指针 [英] dereferencing pointer to incomplete type

查看:32
本文介绍了解引用指向不完整类型的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多关于此的问题,但我将在没有特定代码的情况下提出不同的问题.有没有办法轻松确定导致类型不完整的原因?在我的情况下,我使用的是别人的代码,我完全确定我没有正确的标题,但是(因为计算机比人类眼球更快更好地完成这些事情)有没有办法让编译器说,嘿,您认为在第 34 行输入了 X,但实际上 缺少."错误本身仅在您分配时显示,这不是很有帮助.

I've seen a lot of questions on this but I'm going to ask the question differently without specific code. Is there a way of EASILY determining what is causing the type to be incomplete? In my case I'm using someone elses code and I'm completely sure I don't have the headers right, but (since computers do this stuff much faster and better than human eyeballs) is there a way to get the compiler to say, "hey you think you have type X at line 34 but that's actually missing." The error itself only shows up when you assign, which isn't very helpful.

推荐答案

前几天我看到一个问题,有人通过指定类似

I saw a question the other day where someone inadvertently used an incomplete type by specifying something like

struct a {
    int q; 
}; 
struct A *x; 
x->q = 3;

编译器知道 struct A 是一个结构体,尽管 A 完全未定义,因为 struct 关键字.

The compiler knew that struct A was a struct, despite A being totally undefined, by virtue of the struct keyword.

那是在 C++ 中,struct 的这种用法是不典型的(而且,事实证明,可能会导致误踢).在 C 中,如果你这样做

That was in C++, where such usage of struct is atypical (and, it turns out, can lead to foot-shooting). In C if you do

typedef struct a {
    ...
} a;

然后你可以使用 a 作为类型名,然后省略 struct .如果您输入错误名称或忘记标头,这将导致编译器稍后向您提供未定义的标识符错误,而不是不完整的类型.

then you can use a as the typename and omit the struct later. This will lead the compiler to give you an undefined identifier error later, rather than incomplete type, if you mistype the name or forget a header.

这篇关于解引用指向不完整类型的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆