在C ++中编译错误,而不是C [英] compile error in C++, not C

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

问题描述

以下程序在C中编译得很好,但在C ++中编译不正确。可以

有人解释原因吗?我有一大堆代码可以定义像

这样的东西,在头文件中(没有extern)我无法轻易改变。


C编译器识别出第一个foo和第二个foo一样。

C ++编译器没那么多。


有没有办法让它在C ++中编译而不用改变所有

标题?

int foo;

int foo = 1;


int

main(无效)

{

返回foo;

}

解决方案

8月23日凌晨2:44,Tim H< thoc ... @ gmail.comwrote:


以下程序在C中编译得很好,但在C ++中却没有。可以

有人解释原因吗?我有一大堆代码可以定义像

这样的东西,在头文件中(没有extern)我无法轻易改变。


C编译器识别出第一个foo和第二个foo相同。

C ++编译器不是那么多。



声明是声明,必须是唯一的。 C ++强制执行

,没有任何例外。你建议那个应该筛选所有语句的

程序员来弄清楚某个给定变量的声明是什么。

考虑:

int foo;

double foo(0);


也,你怎么看客户(您的代码的用户)想出上面写的内容是什么意思?如果客户端看到int foo = 1;

并且没有意识到foo在另一个范围内怎么办? poof你的

理论。


>

有没有办法让它在C ++中编译没有更改所有

标题?


int foo;

int foo = 1;


int

main(无效)

{

返回foo;


}


Tim H写道:


C编译器将第一个foo和第二个foo识别为相同。



这只是个人意见,但IMO C语言规范

已被打破。我清楚地看到在那里声明了两个变量,两个都是

同名。


int foo;

int foo = 1;


在文章< be0fbae6-1e9e-4d31-b55d-
69 ********** @ s20g2000prd.googlegroups.com >, th ***** @ gmail.com 说...


以下程序在C中编译得很好,但不是C ++。可以

有人解释原因吗?我有一大堆代码可以定义像

这样的东西,在头文件中(没有extern)我无法轻易改变。


C编译器识别出第一个foo和第二个foo一样。

C ++编译器没那么多。


有没有办法让它在C ++中编译而不用改变所有

标题?


int foo;

int foo = 1;



在C中,每个都是(单独)暂定。您可以根据需要为变量提供尽可能多的暂定定义,因为它们都没有直接与其他任何变量冲突。

在阅读了所有

暂定定义之后,编译器根据1)这些定义的默认值来创建

变量的完整定义

放置,2)所有存储类,限定符等,你把所有的暂定定义放入

。正如我所说的那样,只要你把

放到一个暂定的定义中就会直接与你在另一个中放入的内容相冲突(例如在一个地方将foo定义为一个int而不是双倍在

另一个)这是允许的。例如:


int foo;

extern int foo;

volatile int foo;

int foo = 1;


在这种情况下,整体定义相当于:

extern volatile int foo = 1;


在C ++中,没有一个暂定的定义。有一个

定义规则,说你必须完全定义变量一次

,那就是它。几乎可以肯定的是,没有一种方法可以说服一个C ++编译器接受你的定义,因为它们是正确的

现在。


至于如何使事情有效:我很确定你不会是这样的b $ b能够说服C ++编译器接受源代码,

所以它基本上归结为你是否能找到一个工具来生成你需要的东西。我不确定它是否可以完成这项工作

,但是我看到的第一个可能是Doxygen。


-

后来,

杰瑞。


宇宙是自己想象的虚构。


The following program compiles just fine in C, but not in C++. Can
anyone explain why? I have a chunk of code that defines stuff like
this in headers (without the extern) that I can not easily change.

The C compiler recognizes the first foo and second foo as the same.
The C++ compiler not so much.

Is there a way to get this to compile in C++ without changing all the
headers?
int foo;
int foo=1;

int
main(void)
{
return foo;
}

解决方案

On Aug 23, 2:44 am, Tim H <thoc...@gmail.comwrote:

The following program compiles just fine in C, but not in C++. Can
anyone explain why? I have a chunk of code that defines stuff like
this in headers (without the extern) that I can not easily change.

The C compiler recognizes the first foo and second foo as the same.
The C++ compiler not so much.

A declaration is a declaration and these must be unique. C++ enforces
this with no exceptions whatsoever. You are suggesting that its the
programmer that should sift through all statements to figure out which
(and where) a given variable is declared.

consider:
int foo;
double foo(0);

also, how do you expect the client (the user of your code) to figure
out what you wrote above means? What if the client sees int foo = 1;
and doesn''t realize that foo is in another scope? poof goes your
theory.

>
Is there a way to get this to compile in C++ without changing all the
headers?

int foo;
int foo=1;

int
main(void)
{
return foo;

}


Tim H wrote:

The C compiler recognizes the first foo and second foo as the same.

This is just a personal opinion, but IMO the C language specification
is broken. I clearly see two variables being declared there, both with
the same name.

int foo;
int foo=1;


In article <be0fbae6-1e9e-4d31-b55d-
69**********@s20g2000prd.googlegroups.com>, th*****@gmail.com says...

The following program compiles just fine in C, but not in C++. Can
anyone explain why? I have a chunk of code that defines stuff like
this in headers (without the extern) that I can not easily change.

The C compiler recognizes the first foo and second foo as the same.
The C++ compiler not so much.

Is there a way to get this to compile in C++ without changing all the
headers?
int foo;
int foo=1;

In C, each of these is (separately) a "tentative definition". You can
have as many tentative definitions for a variable as you like, as long
as none of them directly conflicts with any other. After reading all the
tentative definitions, the compiler creates a complete definition of the
variable based on 1) the defaults from where those definitions are
placed, and 2) all the storage classes, qualifiers, etc. you put into
all the tentative definitions. As I said, as long as nothing you put
into one tentative definition directly conflicts with what you put in
another (e.g. defining foo as an int in one place but a double in
another) this is allowed. For example:

int foo;
extern int foo;
volatile int foo;
int foo = 1;

In this case, the overall definition is equivalent to:
extern volatile int foo = 1;

In C++, there''s no such thing as a tentative definition. There''s a one
definition rule that says you have to define the variable exactly once
and that''s it. There''s almost certainly not going to be a way to
persuade a C++ compiler to accept your definitions as they stand right
now.

As to how to make things work: I''m pretty sure you''re not going to be
able to persuade a C++ compiler to accept the source code as it stands,
so it''s basically going to come down to whether you can find a tool to
generate what you need. Offhand I''m not sure whether it can do the job
or not, but the first one I''d look at would probably be Doxygen.

--
Later,
Jerry.

The universe is a figment of its own imagination.


这篇关于在C ++中编译错误,而不是C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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