解决警告错误 [英] resolving a warning error

查看:128
本文介绍了解决警告错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个返回枚举的原型,但不要声明

枚举。


这适用于多个系统和编译器,但是在aix xlc上失败。


以下是示例代码:


enum anEnum someFunc(void);

我运行时


xlc -c someFunc.c

i get

1506-103(W)标签anEnum需要完整之前的定义是

使用。


显然,我可以声明枚举,但

我想避免声明enum我希望

隐藏不需要它的函数的枚举值。


我使用类似的技术来隐藏结构的成员;我只是通过

指向结构的指针,编译器不需要完整的

定义

的结构。


如何在不消除其他

警告消息且不声明枚举的情况下消除该警告消息?

I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don''t need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?

推荐答案

michael potter写道:
michael potter wrote:
我想编写一个返回枚举的原型,但不要声明枚举。

这可以在几个系统和编译器,但在aix xlc上失败。

以下是示例代码:

枚举anEnum someFunc(void);

当我运行:
xlc -c someFunc.c
我得到了
1506-103(W)标签anEnum在使用之前需要一个完整的定义。

显然,我可以声明枚举,但是我想避免声明枚举,因为我希望将枚举的值隐藏在不需要它的函数中。

我用的是simi隐藏结构成员的技术;我只是将指针传递给结构体,并且编译器不需要结构的完整定义。

如何在不消除的情况下消除该警告消息其他
警告信息并没有声明枚举?
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.

Here is the sample code:

enum anEnum someFunc(void);

when i run:
xlc -c someFunc.c
i get
1506-103 (W) Tag anEnum requires a complete definition before it is
used.

Obviously, I could declare the enum, but
I would like to avoid declaring the enum as I would like
to hide the values of the enum from functions that don''t need it.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition
of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?




使用`int someFunc(void);''代替。


但我会失去类型安全!好吧,不。在第一个

的地方,开始几乎没有类型安全:C'的

枚举非常弱。第二,如果你打算用
隐藏来自someFunc()的枚举值,它的

`return''语句会是什么样子?你将在哪里获得

一个已知的有效值?从全球复制?呸!


或许您打算将枚举值显示为

someFunc()但是将它们隐藏在someFunc()的调用者中。什么是
的重点?这意味着所有的调用者都可以使用someFunc()获得返回值,并且在没有理解的情况下将它传递给

本身:他们不能执行

使用if或switch进行有意义的测试或任何此类;

所有他们能做的就是把它从一个地方交给另一个地方

" opaque"值。 int非常适合这种用途。


`enum anEnum''似乎无法在这里增值。


-
Er*********@sun.com



By using `int someFunc(void);'' instead.

"But I''ll lose type safety!" Well, no. In the first
place, there''s almost no type safety to begin with: C''s
enums are pretty weak. In the second place, if you intend
to hide the enum''s values from someFunc(), what will its
`return'' statements look like? Where are you going to get
a known-to-be-valid value? Copy it from a global? Bah!

Or perhaps you intend to reveal the enum values to
someFunc() but hide them from someFunc()''s callers. What''s
the point of that? It means that all the callers can do
with someFunc()''s returned value is pass it around among
themselves without understanding: They can''t perform a
meaningful test with an `if'' or a `switch'' or any such;
all they can do is hand it from place to place as an
"opaque" value. An `int'' is perfectly suited to such use.

`enum anEnum'' doesn''t seem to add value here.

--
Er*********@sun.com




文章< 23 ******************** ****** @ post.google.com>, po****@gmail.com (迈克尔波特)写道:

In article <23**************************@posting.google.com >, po****@gmail.com (michael potter) writes:
我想编写一个返回枚举的原型,但不会声明枚举。

我使用类似的技术隐藏成员结构;我只是将指针传递给结构,并且编译器不需要完整的结构定义。

如何在不消除其他结构的情况下消除该警告消息>警告信息并且没有声明枚举?
I would like to code a prototype that returns an enum, but not declare
the enum.

I use a similar technique to hide the members of a struct; I just pass
the pointer to the struct and the compiler does not require a full
definition of the struct.

How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?




你不能。好吧,你可能能够在你的最后一个问题中专门做你所要求的

- 从一个特定的

实现中消除一个诊断 - 但这不是主题。你不能做的是

使用枚举而不用声明它,在C.C允许不完整的结构

在各种情况下;它不允许不完整的枚举。一些

实现可能允许后者,但它是一个扩展。


有一个很好的理由不允许不完整的枚举 -

实现允许为枚举选择任何整数类型,

,前提是该类型可以表示枚举的所有值。

后者总是int类型,但是它们可能适合于,例如,

unsigned char,在这种情况下实现可以使用unsigned

用于该枚举类型的字符。


如果实现没有完整的枚举定义,

它可能不知道是什么用于它的整数类型。


当然,这并不排除指向不完整枚举的指针,只是

,因为你可以指向不完整的结构。为什么那些被省略的是b $ b对我来说是一个谜;我想委员会中没有人认为

他们足够重要。


-

Michael Wojcik mi ************ @ microfocus.com


口袋#16:Ventriloquist'''Helper" - 为潜水员提供录音,

尤其是要塞进敌人的口中 - 上帝保佑队长

Vere! Les jeux sont faits!和C。 - Joe Green



You can''t. Well, you may be able to do specifically what you ask
in your final question - eliminate one diagnostic from one particular
implementation - but that''s off-topic here. What you cannot do is
use an enum without declaring it, in C. C permits incomplete structs
in various contexts; it does not permit incomplete enums. Some
implementations may allow the latter, but it is an extension.

There''s a good reason for not allowing incomplete enums - the
implementation is allowed to pick any integral type for an enum,
provided that type can represent all the values of the enum. The
latter are always of type int, but they may fit in the range of, say,
unsigned char, in which case the implementation could use unsigned
char for that enum type.

If the implementation doesn''t have a complete definition of the enum,
it may not know what integral type to use for it.

Of course, that doesn''t preclude pointers to incomplete enums, just
as you can have pointers to incomplete structs. Why those were
omitted is a mystery to me; I suppose no one on the committee thought
they were sufficiently important.

--
Michael Wojcik mi************@microfocus.com

Pocket #16: A Ventriloquist''s "Helper" -- Recordings for Divers Occasions,
especially cries to put in the mouths of enemies -- "God Bless Captain
Vere!" "Les jeux sont faits!" &c. -- Joe Green


In< 23 ************************** @ posting.google.com> po****@gmail.com (michael potter)写道:
In <23**************************@posting.google.com > po****@gmail.com (michael potter) writes:
I想要编写返回枚举的原型,但不要声明枚举。

这适用于多个系统和编译器,但在aix xlc上失败。


如果你以符合模式调用它们,那就没有了:


fangorn:〜/ tmp 200> gcc test.c

fangorn:〜/ tmp 201> icc test.c

fangorn:〜/ tmp 202> gcc -ansi -pedantic test.c

test.c:在函数`main'':

test.c:8:警告:ISO C禁止前向引用` enum''类型

fangorn:〜/ tmp 203> icc -Xc test.c

test.c(8):警告#102:枚举类型的前向声明是非标准的

枚举anEnum someFunc(void);

^

诊断符合*标准* * *


6.7.2.3标签


约束


1特定类型的内容最多只能定义一次。


2表格的类型说明符/>

enum标识符

没有枚举器列表的
只能出现在

指定的类型完成之后。

如何在不消除其他
警告信息的情况下消除该警告信息并且不声明枚举?
I would like to code a prototype that returns an enum, but not declare
the enum.

This works on several systems and compilers, but fails on aix xlc.
Not if you invoke them in conforming mode:

fangorn:~/tmp 200> gcc test.c
fangorn:~/tmp 201> icc test.c
fangorn:~/tmp 202> gcc -ansi -pedantic test.c
test.c: In function `main'':
test.c:8: warning: ISO C forbids forward references to `enum'' types
fangorn:~/tmp 203> icc -Xc test.c
test.c(8): warning #102: forward declaration of enum type is nonstandard
enum anEnum someFunc(void);
^
The diagnostic is *required* by the C standard:

6.7.2.3 Tags

Constraints

1 A specific type shall have its content defined at most once.

2 A type specifier of the form

enum identifier

without an enumerator list shall only appear after the type it
specifies is complete.
How can I eliminate that warning message without eliminating other
warning messages and without declaring the enum?




你唯一的机会是找到一个在不合格模式下调用xlc的方式,

,因为一个符合标准的编译器必须生成它,但是你真的*不要
想要这样做。


Dan

-

Dan Pop

DESY Zeuthen,RZ group

电子邮件: Da*****@ifh.de

目前正在寻找在欧盟工作



Your only chance is to find a way of invoking xlc in nonconforming mode,
because a conforming compiler MUST generate it, but you *really* don''t
want to do that.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union


这篇关于解决警告错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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