编译问题 [英] Compiling problem

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

问题描述

我的情况非常复杂,并且它不想

编译。

情况如下:

我有三个课程,让我们称他们为A,B和C.他们每人都有一个.h

和一个.cpp文件。

所有标题使用以下构造来确保标题是

不包括多次:

#ifndef _A_H_

#define _A_H_

A级{

...

};

#endif

A有静态成员B级,

B有一个C类成员,用一些

方法处理A类对象,

和C有一个A级成员。


当我尝试编译这个时,我得到的错误是没有上课时

当C正在编译时,等等。我怎么能解决这个问题?

I''m having a quite complicate situation, and it doesn''t want to
compile.
The situation is as follows:
I have three classes, let''s call them A, B and C. They each have a .h
and a .cpp file.
All headers use the following construct to make sure the headers are
not included multiple times:
#ifndef _A_H_
#define _A_H_
class A {
...
};
#endif
A has a static member of class B,
B has a member of class C and works with objects of class A in some
methods,
and C has a member of class A.

When i try to compile this, i get errors saying that there is no class
A when C is compiling, and so on. How could i fix this?

推荐答案

* Barrie:
我有一个相当复杂的情况,并且它不想编译。
情况如下:
我有三个班,让我们称他们为A,B和C.他们每个人都有a .h
和.cpp文件。
所有标题都使用以下构造来确保标题不被多次包含:
#ifnf _A_H_


以下划线后跟大写字母开头的名称保留给

实现;你已经有了未定义的行为。

#define _A_H_
A类{
...
};
#endif

A有一个B类的静态成员,
B有一个C类成员,在某些方法中使用A类对象,
和C有A类成员。当我尝试编译这个时,我得到的错误是在C正在编译时没有类
A,依此类推。我怎么能解决这个问题?
I''m having a quite complicate situation, and it doesn''t want to
compile.
The situation is as follows:
I have three classes, let''s call them A, B and C. They each have a .h
and a .cpp file.
All headers use the following construct to make sure the headers are
not included multiple times:
#ifndef _A_H_
Names starting with an underscore followed by uppercase are reserved for
the implementation; you have Undefined Behavior already.
#define _A_H_
class A {
...
};
#endif
A has a static member of class B,
B has a member of class C and works with objects of class A in some
methods,
and C has a member of class A.

When i try to compile this, i get errors saying that there is no class
A when C is compiling, and so on. How could i fix this?




最好的方法:修复你的设计,这是绝望的,+读取C ++作为

语言,你需要学习命名规则等基本内容(参见上面的
)。


或者从技术上讲,这是你能做的最糟糕的事情,包括适当的

在合适的位置标题并使用前向声明使你的b / b
当前糟糕/邪恶的设计编译;有关示例,请参阅常见问题解答。如果你选择这样做,那么很有可能在编译之后,

并且在修复上面提到的名称问题之后,你仍然会有UB

由于静态初始化顺序问题。并且有一个更好的机会,除非其他东西爆炸,否则你不会注意到。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



The best way: fix your design, which is hopeless, + read up on C++ as a
language, where you need to learn such basic things as naming rules (see
above).

Or technically, which is the worst you can do, include the proper
headers at the right places and use forward declarations to make your
current bad/evil design compile; see the FAQ for example(s). If you
choose to do this, then there is a good chance that after it compiles,
and after fixing the name issue mentioned above, you''ll still have UB
due to static initialization order problems. And there is an even
better chance that you won''t notice until something else blows up.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


2006年3月18日02:58:29 -0800,"巴里" < BA ******** @ gmail.com>写道:
On 18 Mar 2006 02:58:29 -0800, "Barrie" <ba********@gmail.com> wrote:
我的情况非常复杂,而且它不想编译。
情况如下:和一个.cpp文件。
所有标题使用以下结构来确保标题
不包括多次:
#ifnf _A_H_
#define _A_H_
A类{
...
};
#endif

A有一个B类的静态成员,
B有一个C类成员,在一些
方法中使用A类对象,
和C有A类的成员。

当我尝试编译这个时,我得到的错误是说C语言正在编译时没有类别,等等。我怎么能解决这个问题?


如果您将这些

对象作为成员,则始终存在循环依赖性问题。正如Alf P. Steinbach所指出的那样,像

这样的设计永远不会起作用。


编译类声明时,编译器必须知道有多大

对象将在结束括号后的分号

达到。这意味着对于所有班级成员,每个

成员的大小必须是已知的。如果你有一个指针或引用成员,

编译器可以接受它而不包括那个类型的头,因为

指针的大小是已知的而不知道类型(在某些情况下,

指向成员的指针除外)。所有人必须做的是

写一个类名称的前向声明,例如:


A级;

B级{

// B可以使用名称,但不能使用类型:

A * pa;

} ;


对于类类型的成员变量,每个标题必须包含

这些成员的标题。 IOW,

A有一个B类静态成员,
....意味着A的标题必须包含B的标题;

B有一个成员对于C类而言,在某些方法中使用A类对象,
....意味着B'的标题必须包含C'的标题,但不一定是

A'的标题,因为在

方法的定义之前不需要它(你应该放在一个单独的.cpp文件中);

和C有一个A类成员。
I''m having a quite complicate situation, and it doesn''t want to
compile.
The situation is as follows:
I have three classes, let''s call them A, B and C. They each have a .h
and a .cpp file.
All headers use the following construct to make sure the headers are
not included multiple times:
#ifndef _A_H_
#define _A_H_
class A {
...
};
#endif
A has a static member of class B,
B has a member of class C and works with objects of class A in some
methods,
and C has a member of class A.

When i try to compile this, i get errors saying that there is no class
A when C is compiling, and so on. How could i fix this?
You will always have circular dependency issues if you have these
objects as members. As Alf P. Steinbach has pointed out, a design like
this will never work.

When a class declaration is compiled, the compiler must know how large
the object will be by the time the semicolon after the closing brace
is reached. That means that for all class members, the size of each
member must be known. If you have a pointer or reference member, the
compiler can accept it without including that type''s header because
the size of a pointer is known without knowing the type (except for
pointer-to-member under certain circumstances). All one must do is to
write a forward-declaration of the class'' name, e.g.:

class A;

class B {
// B can use the name, but not the type:
A* pa;
};

For member variables of class type, each header will have to include
the headers for those members. IOW,
A has a static member of class B, ....means that header for A must include header for B;
B has a member of class C and works with objects of class A in some
methods, ....means that B''s header must include C''s header, but not necessarily
A''s header because it isn''t needed until the definitions of the
methods (which you should put in a separate .cpp file);
and C has a member of class A.



....意味着C'的标题必须包含A'的标题。但是等一下

.... A需要B,需要C,需要A ......你知道这是什么地方

会怎样?这是一个循环依赖。


一个可能的解决方案,如果你无法避免依赖,是使用

指针或引用成员而不是对象,并转发-declare

该类而不是包括类头。你最终将需要包含标题,但你可以将它们包含在.cpp

文件中。


-

Bob Hairgrove
没有********** @ Home.com


这篇关于编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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