循环依赖 - 我认为...... [英] Circular dependency - I think..

查看:64
本文介绍了循环依赖 - 我认为......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我陷入了循环依赖问题。我以为

前方声明是我问题的解决方案,但我很遗憾

错了(perphaps我今天在实验室工作太久了) 。

FOO的头文件由BAR组成,同样BAR组成

of FOO。

如果我命令FOO.cpp,Visual Studio'的第一个投诉(C2146语法

错误,丢失;在foo之前 - 下一个投诉是BAR :: FOO缺少

存储类型)指向FOO的实例化在BAR'的标题中(即$ F $ b线FOO foo)。类似地,如果我编译BAR Visual第一次投诉

(C2146语法错误,丢失;之前吧 - 下一次投诉是

FOO :: BAR缺少存储类型)指向实例化BAR中

FOO'的标题(即BAR栏)。


解决方案??


提前致谢。


守则。

#ifndef FOO_H

#define FOO_H

#include" bar.h"

//班级BAR;


班级FOO

{

public:

FOO();

~FOO();

int GetFbk();

void ComputeTorquerCmd();

private:

int idx;

void SetInUse();

吧栏;

};


#endif

#include" foo.h"

#include< iostream>


FOO :: FOO():idx(0)

{

std :: cout<< " foo的构造函数叫做 << std :: endl;

}


FOO ::〜FOO()

{

std :: cout<< " foo destructing << std :: endl;

}


int FOO :: GetFbk()

{

return ++ idx; //用于演示目的

}


void FOO :: ComputeTorquerCmd()

{

}


void FOO :: SetInUse()

{

bar.SetInUse();

std :: cout<< " foo在使用中设置 << std :: endl;

}

//////////////////////////// //////////////


#ifndef BAR_H

#define BAR_H

#include foo.h


//类FOO;

类BAR

{

公开:

BAR();

~BAR();

void GetPosFbkFoo();

int GetInUse ()const;

void SetInUse();


private:

int in_use;

FOO foo;

};


#endif


#include" bar.h"

#include< iostream>


BAR :: BAR():in_use(0)

{

std :: cout<< " bar'的构造函数" << std :: endl;

}

BAR ::〜BAR(){}


void BAR :: GetPosFbkFoo()

{

SetInUse();

if(in_use)

{

int jdx = foo.GetFbk();

std :: cout<< jdx<< std :: endl;

}

else //做其他事情

{

foo.ComputeTorquerCmd() ;

}

}


int BAR :: GetInUse()const

{

返回in_use;

}


void BAR :: SetInUse()

{

in_use ^ = 1;

}

另外,我正在使用谷歌的新闻阅读器限制我看到

瞬间回应(谨慎使用)。关键是,我的后续工作

可能会迟到。


一方面,我的顾问告诉我声音设计限制
''refractoring''。后来他告诉我'拥抱改变''。我很困惑。

解决方案



" ma740988" <毫安****** @ pegasus.cc.ucf.edu>在消息中写道

新闻:a5 ************************* @ posting.google.co m ... < blockquote class =post_quotes>我认为我陷入了循环依赖问题。我以为
前向声明是我问题的解决方案,但我很遗憾地错了(perphaps我今天已经在实验室工作太久了。)
头文件对于FOO由BAR组成,同样BAR由FOO组成。
如果我恭维FOO.cpp,Visual Studio的第一个抱怨(C2146语法错误,丢失;在foo之前 - 下一个投诉是BAR :: FOO缺少
存储类型)指向BAR'标题中FOO的实例化(即线FOO foo)。类似如果我编译BAR Visual第一次投诉
(C2146语法错误,缺失;之前吧 - 下一个抱怨是FOO :: BAR缺少存储类型)指向BAR的实例化在
FOO '标题(即BAR栏)。

解决方案??

提前致谢。

守则。
#ifndef FOO_H
#define FOO_H

#include" bar.h"
//班级BAR;

班级FOO
{
公开:
FOO();
~FOO();
int GetFbk();
void ComputeTorquerCmd();
private:
int idx;
void SetInUse();
BAR bar;




使用指向BAR的指针。 />

参见:
http://www.parashift.com/c++-faq-lit...html#faq-38.12


问候,

Sumit。

-

Sumit Rajan< su ******** @ myr ealbox.com>


2004年7月25日17:33:01 -0700,ma740988< ma ****** @ pegasus.cc.ucf.edu>

写道:

我认为我陷入了循环依赖问题。我以为
前向声明是我问题的解决方案,但我很遗憾地错了(perphaps我今天已经在实验室工作太久了。)
头文件对于FOO由BAR组成,同样BAR由FOO组成。
如果我恭维FOO.cpp,Visual Studio的第一个抱怨(C2146语法错误,丢失;在foo之前 - 下一个投诉是BAR :: FOO缺少
存储类型)指向BAR'标题中FOO的实例化(即线FOO foo)。类似如果我编译BAR Visual第一次投诉
(C2146语法错误,缺失;之前吧 - 下一个抱怨是FOO :: BAR缺少存储类型)指向BAR的实例化在
FOO '标题(即BAR栏)。

解决方案??




没有。一个对象不能包含它自己,同样一个对象不能包含一个包含自身的对象。使用一个或两个指针

FOO和BAR。


john


ma740988写道:


我认为我陷入了循环依赖问题。我以为
前向声明是我问题的解决方案,但我很遗憾地错了(perphaps我今天已经在实验室工作太久了。)
头文件对于FOO由BAR组成,同样BAR由FOO组成。
如果我恭维FOO.cpp,Visual Studio的第一个抱怨(C2146语法错误,丢失;在foo之前 - 下一个投诉是BAR :: FOO缺少
存储类型)指向BAR'标题中FOO的实例化(即线FOO foo)。类似如果我编译BAR Visual第一次投诉
(C2146语法错误,缺失;之前吧 - 下一个抱怨是FOO :: BAR缺少存储类型)指向BAR的实例化在
FOO标题(即BAR栏)。




给予


等级FOO

{

int i;

BAR bar;

}


class BAR
{

int j;

FOO foo

};


现在回答一个简单的问题:

FOO对象的大小是多少。假设sizeof(int)== 4


好​​吧:一个FOO对象由一个int组成,它将是
为4个字节加上一个BAR对象的大小。那个

将是另外4个字节加上FOO对象的大小。

其他4个字节加上BAR的大小

宾语。另外4个字节加上FOO对象的大小。

再添加4个字节加上BAR对象的大小....

(ad infinitum)


答案:FOO对象的大小不限。我非常确定你的电脑没有那么多记忆。


-

Karl Heinz Buchegger
kb******@gascad.at


I think I''m caught up in a circular dependency problem. I''d thought
forward declaration was the solution to my problem but I''m sadly
mistaken (perphaps i''ve been in the lab toooo long today).
The header file for FOO is composed of BAR, likewise BAR is composed
of FOO.
If I complile FOO.cpp, Visual Studio''s first complaint (C2146 syntax
error, missing ; before foo - the next complaint is BAR::FOO missing
storage type) points to the instantiation of FOO in BAR''s header(i.e
the line FOO foo). Similarily if I compile BAR Visual first complaint
(C2146 syntax error, missing ; before bar - the next complaint is
FOO::BAR missing storage type) points to the instantiation of BAR in
FOO''s header (i.e the line BAR bar).

The solution??

Thanks in advance.

The Code.
#ifndef FOO_H
#define FOO_H

# include "bar.h"
//class BAR;

class FOO
{
public:
FOO();
~FOO();
int GetFbk();
void ComputeTorquerCmd();
private:
int idx;
void SetInUse();
BAR bar;
};

#endif
# include "foo.h"
# include <iostream>

FOO::FOO() : idx(0)
{
std::cout << " foo''s constructor called " << std::endl;
}

FOO::~FOO()
{
std::cout << " foo destructing " << std::endl;
}

int FOO::GetFbk()
{
return ++idx; // for demo purposes
}

void FOO::ComputeTorquerCmd()
{
}

void FOO::SetInUse()
{
bar.SetInUse();
std::cout << " foo setting in use " << std::endl;
}
//////////////////////////////////////////

#ifndef BAR_H
#define BAR_H
# include "foo.h"

//class FOO;
class BAR
{
public:
BAR();
~BAR();
void GetPosFbkFoo();
int GetInUse() const;
void SetInUse();

private:
int in_use;
FOO foo;
};

#endif

# include "bar.h"
# include <iostream>

BAR::BAR() : in_use(0)
{
std::cout << " bar''s constructor " << std::endl;
}
BAR::~BAR() {}

void BAR::GetPosFbkFoo()
{
SetInUse();
if (in_use)
{
int jdx = foo.GetFbk();
std::cout << jdx << std::endl;
}
else // do something else
{
foo.ComputeTorquerCmd();
}
}

int BAR::GetInUse() const
{
return in_use;
}

void BAR::SetInUse()
{
in_use ^= 1;
}
An aside, I''m using google''s newsreader which limits me from seeing a
response (used sparingly) momentarily. The point being, my follow ups
may be ''late''.

On the one hand my advisor tells me a sound design limits
''refractoring''. Later he tells me ''embrace change''. I''m confused.

解决方案


"ma740988" <ma******@pegasus.cc.ucf.edu> wrote in message
news:a5*************************@posting.google.co m...

I think I''m caught up in a circular dependency problem. I''d thought
forward declaration was the solution to my problem but I''m sadly
mistaken (perphaps i''ve been in the lab toooo long today).
The header file for FOO is composed of BAR, likewise BAR is composed
of FOO.
If I complile FOO.cpp, Visual Studio''s first complaint (C2146 syntax
error, missing ; before foo - the next complaint is BAR::FOO missing
storage type) points to the instantiation of FOO in BAR''s header(i.e
the line FOO foo). Similarily if I compile BAR Visual first complaint
(C2146 syntax error, missing ; before bar - the next complaint is
FOO::BAR missing storage type) points to the instantiation of BAR in
FOO''s header (i.e the line BAR bar).

The solution??

Thanks in advance.

The Code.
#ifndef FOO_H
#define FOO_H

# include "bar.h"
//class BAR;

class FOO
{
public:
FOO();
~FOO();
int GetFbk();
void ComputeTorquerCmd();
private:
int idx;
void SetInUse();
BAR bar;



Use a pointer to BAR instead.

See:
http://www.parashift.com/c++-faq-lit...html#faq-38.12

Regards,
Sumit.
--
Sumit Rajan <su********@myrealbox.com>


On 25 Jul 2004 17:33:01 -0700, ma740988 <ma******@pegasus.cc.ucf.edu>
wrote:

I think I''m caught up in a circular dependency problem. I''d thought
forward declaration was the solution to my problem but I''m sadly
mistaken (perphaps i''ve been in the lab toooo long today).
The header file for FOO is composed of BAR, likewise BAR is composed
of FOO.
If I complile FOO.cpp, Visual Studio''s first complaint (C2146 syntax
error, missing ; before foo - the next complaint is BAR::FOO missing
storage type) points to the instantiation of FOO in BAR''s header(i.e
the line FOO foo). Similarily if I compile BAR Visual first complaint
(C2146 syntax error, missing ; before bar - the next complaint is
FOO::BAR missing storage type) points to the instantiation of BAR in
FOO''s header (i.e the line BAR bar).

The solution??



There is none. An object cannot contain itself, similarly an object cannot
contain an object which contains itself. Use a pointer for one or both of
FOO and BAR.

john


ma740988 wrote:


I think I''m caught up in a circular dependency problem. I''d thought
forward declaration was the solution to my problem but I''m sadly
mistaken (perphaps i''ve been in the lab toooo long today).
The header file for FOO is composed of BAR, likewise BAR is composed
of FOO.
If I complile FOO.cpp, Visual Studio''s first complaint (C2146 syntax
error, missing ; before foo - the next complaint is BAR::FOO missing
storage type) points to the instantiation of FOO in BAR''s header(i.e
the line FOO foo). Similarily if I compile BAR Visual first complaint
(C2146 syntax error, missing ; before bar - the next complaint is
FOO::BAR missing storage type) points to the instantiation of BAR in
FOO''s header (i.e the line BAR bar).



given

class FOO
{
int i;
BAR bar;
}

class BAR
{
int j;
FOO foo
};

Now answer a quick question:
What is the size of a FOO object. Assume sizeof(int) == 4

Well: a FOO object consists of an int, that would
be 4 bytes plus the size of an BAR object. That
would be another 4 bytes plus the size of an FOO object.
Which accounts for another 4 bytes plus the size of an BAR
object. Another 4 bytes plus the size of a FOO object.
Add another 4 bytes plus the size of an BAR object ....
(ad infinitum)

Answer: A FOO object would be of unlimited size. I''m quite
sure that your computer doesn''t have that much memory.

--
Karl Heinz Buchegger
kb******@gascad.at


这篇关于循环依赖 - 我认为......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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