无法编译简单的例子 [英] Cannot compile simple example

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

问题描述

嗨!

为什么下面这个简单的例子产生了这样的错误?

我知道它与两个头文件有关,包括彼此和

(另外)在对立中使用类别1和类别2。标题

文件...我需要做些什么来使这个例子有效?


感谢您的帮助!

// *错误消息:

One.h(11):错误C2146:语法错误:在标识符t之前缺少'';''

One.h(11):错误C2501:''One :: Two'':缺少存储类或类型

说明符

One.h(11 ):错误C2501:''One :: t'':缺少存储类或类型说明符

Two.h(11):错误C2146:语法错误:在标识符之前缺少'';'' 't''

// *文件One.h

#ifndef ONE_H

#define ONE_H


#include" Two.h"


class One

{

public:

一();

私人:

两个;

};


#endif


// *文件One.cpp

#include" One.h"


一::一个()

{

}


// *文件Two.h

#ifndef TWO_H

#define TWO_H


#include" One.h"


class 2

{

public:

Two();

private :

一个;

};


#endif


// *文件Two.cpp

#include" Two.h"


两个::两个()

{

}


// *文件Main.cpp

int main()

{

返回0;

}

Hi!
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other and
(moreover) the usage of the classes One and Two in the "opposite" header
file... What do I have to do to make this example work?

Thanks for all the help!

// * Error messages:
One.h(11): error C2146: syntax error : missing '';'' before identifier ''t''
One.h(11): error C2501: ''One::Two'' : missing storage-class or type
specifiers
One.h(11): error C2501: ''One::t'' : missing storage-class or type specifiers
Two.h(11): error C2146: syntax error : missing '';'' before identifier ''t''
// * File One.h
#ifndef ONE_H
#define ONE_H

#include "Two.h"

class One
{
public:
One();
private:
Two t;
};

#endif

// * File One.cpp
#include "One.h"

One::One()
{
}

// * File Two.h
#ifndef TWO_H
#define TWO_H

#include "One.h"

class Two
{
public:
Two();
private:
One t;
};

#endif

// * File Two.cpp
#include "Two.h"

Two::Two()
{
}

// * File Main.cpp
int main()
{
return 0;
}

推荐答案

我也是新手。所以请耐心等待。


但你的头文件是CIRCULAR。


定义另一个。逐行走过,有很好的

混乱。


你必须理顺指令序列,所以没有

歧义。然后它看起来应该编译好。


- 罗伯特 -

I''m new to this, too. So bear with me.

But your header files are CIRCULAR.

one defines the other. Walk through line by line and there is great
confusion.

You must straighten out the sequence of instructions so there is NO
ambiguity. Then it looks like it should compile ok.

- Robert -


" Mtk" < SP *********** @ nra.org>在留言中写道

news:di ********** @ news.island.liu.se ...
"Mtk" <sp***********@nra.org> wrote in message
news:di**********@news.island.liu.se...
为什么下面,简单,示例产生这样的错误?
我知道它与两个头文件有关,包括彼此


由于包含警卫,这两个文件看起来只包括每个

其他。实际上,其中一个包括另一个;就是这样。 (第二个

也包括第一个,但那时第一个

标题的正文包含在内。)

和(另外)在对面
头文件中使用类One和Two ...我需要做些什么才能使这个例子有效?


你必须使用前向声明,并且必须在另一个标题中使用其中一个类作为

引用(意思是引用和指针)。 />
// *错误消息:
One.h(11):错误C2146:语法错误:在标识符t之前缺少'';''
One.h( 11):错误C2501:''One :: Two'':缺少存储类或类型
说明符
One.h(11):错误C2501:''One :: t'':缺失存储类或类型
说明符
Two.h(11):错误C2146:语法错误:在标识符t之前缺少'';''

// *文件One.h
#ifndef ONE_H
#define ONE_H

#include" Two.h"

第一课
{
公开:
一个();
私人:
两个t;


这条线很好;因为我们知道两个是什么。

};

#endif

// *文件One.cpp
#include" One.h

One :: One()
{
}
// *文件Two.h
#ifndef TWO_H
#define TWO_H

#include" One.h"


上面的行是无效的,因为宏ONE_H是在这个

点定义的,所以我们在上面的行中包含的是什么都没有。 ;

二级
{
公开:
二();
私人:
一个;


您在上面的行中有一个概念性问题。

两个对象彼此包含是不可能的;这将是无限递归的。


至少有一个类必须引用。到另一个;它不应该是b $ b包括它作为一个部分。

};

#endif
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other
Because of the "include guards," the two files only look like including each
other. In effect, one of them includes the other one; and that''s it. (Second
one includes the first one too, but at that time the body of the first
header is included as nothing.)
and (moreover) the usage of the classes One and Two in the "opposite"
header file... What do I have to do to make this example work?
You have to use a forward declaration and must use one of the classes as
reference (meaning both reference and pointer) in the other header.
// * Error messages:
One.h(11): error C2146: syntax error : missing '';'' before identifier ''t''
One.h(11): error C2501: ''One::Two'' : missing storage-class or type
specifiers
One.h(11): error C2501: ''One::t'' : missing storage-class or type
specifiers
Two.h(11): error C2146: syntax error : missing '';'' before identifier ''t''
// * File One.h
#ifndef ONE_H
#define ONE_H

#include "Two.h"

class One
{
public:
One();
private:
Two t;
That line is good; because we know what Two is.
};

#endif

// * File One.cpp
#include "One.h"

One::One()
{
}

// * File Two.h
#ifndef TWO_H
#define TWO_H

#include "One.h"
The line above is ineffective, because the macro ONE_H is defined at this
point, so what we include with the line above is "nothing."
class Two
{
public:
Two();
private:
One t;
You have a conceptual problem on the line above. It is not possible to have
two objects include each other; that would be infinite recursively.

At least one of your classes must "refer" to the other one; it should not
include it as a part.
};

#endif




如果不知道您的确切要求,这里有一个解决方案:


不要包含One.h。在Two.h;相反,转发声明一。然后,保持一个两个One对象的

引用,不包含一个:


// *文件Two.h

#ifndef TWO_H

#define TWO_H


一级; //< - 前瞻性声明


class two

{

public:

explicit两个(一个和一个)//< - 参考一个



t(一个)//< - 记住你的一个

{}

私人:

One&吨; //< - 保留参考


};


#endif


By顺便说一句,我认为这必须是常见问题解答。


阿里



Without knowing your exact requirements, here is one solution:

Don''t include "One.h" in Two.h; instead, forward declare One. Then, keep a
reference to a One object in Two, don''t contain one:

// * File Two.h
#ifndef TWO_H
#define TWO_H

class One; // <-- Forward declaration

class Two
{
public:
explicit Two(One & one) // <-- Take a reference
:
t(one) // <-- Remember your One
{}

private:
One & t; // <-- Keep a reference

};

#endif

By the way, I think this must be an FAQ.

Ali


Mtk写道:
为什么以下这个简单的例子产生了这样的错误?
我知道它与两个头文件包括彼此和
(而且)类一和二的用法有关在对立中标题
文件...我需要做些什么来使这个例子有效?


你不能。


让我们将你的例子压缩成一个文件。让我们假设编译器

知道类''一''和''两''某种方式_apriori_。

[...]
一级
{
公开:
一();
私人:
两个t;
};
[...] <第二课
{
公开:
二();
私人:
一个;
};
Why does the following, simple, example produce such errors?
I know it has to do with the two header files including each other and
(moreover) the usage of the classes One and Two in the "opposite" header
file... What do I have to do to make this example work?
You can''t.

Let''s condense your example to one file. And let''s suppose the compiler
knows about classes ''One'' and ''Two'' somehow _apriori_.
[...]
class One
{
public:
One();
private:
Two t;
};
[...]
class Two
{
public:
Two();
private:
One t;
};



现在,一个对象的大小是多少?一旦你拿出一个公式编译器可以用来计算它,请告诉我们。


同时,查看前向声明 ;。


V



Now, what is the size of a class One object? As soon as you can come up
with a formula the compiler can use to calculate it, let us know.

Meanwhile, look up "forward declaration".

V


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

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