简体   繁体   English

C ++中的循环依赖(?)

[英]circular dependency(?) in C++

My initial suspicion was that there was a circular dependency in my code and went through Resolve header include circular dependencies . 我最初的怀疑是我的代码中存在循环依赖,并且通过Resolve头包含循环依赖 But this hasn't resolved my compilation errors. 但这并没有解决我的编译错误。 Here is the code with 3 classes - A, G & N. 这是包含3个类的代码 - A,G和N.

//A.h

#ifndef A_H_
#define A_H_

class com::xxxx::test::G;

namespace com { namespace xxxx { namespace test {

class A {

public:
 A();
 ~A();
 void method1(void);

private:
 G* g;
};

} } }

#endif /* A_H_ */


//A.cpp

#include "A.h"
#include "G.h"

namespace com { namespace xxxx { namespace test {

A::A() {
 g = new com::xxxx::test::G();
}

A::~A() {
 delete g;
}

void A::method1() {
 g->method2(*this);
}

} } }


//G.h

#ifndef G_H_
#define G_H_

class com::xxxx::test::A;

namespace com { namespace xxxx { namespace test {

class G {
public:
 void method2(const A&);
};

} } }

#endif /* G_H_ */


//G.cpp

#include "N.h"

namespace com { namespace xxxx { namespace test {

void G::method2(const A& a) {
 N n(a, *this);
}

} } }


//N.h

#ifndef N_H_
#define N_H_

#include "A.h"
#include "G.h"

namespace com { namespace xxxx { namespace test {

class N {
public:
 N(const A& obj1, const G& obj2) : a(obj1), g(obj2) {}
 void method3(void);

private:
 A a;
 G g;
};

} } }

#endif /* N_H_ */

I am getting about 10 compilation errors in Ah and A.cpp I am listing the compilation errors below: 我在Ah和A.cpp中得到大约10个编译错误我列出了下面的编译错误:

./src/A.h:11: error: 'com' has not been declared
../src/A.h:25: error: ISO C++ forbids declaration of 'G' with no type
../src/A.h:25: error: invalid use of '::'
../src/A.h:25: error: expected ';' before '*' token
../src/A.cpp: In constructor 'com::xxxx::test::A::A()':
../src/A.cpp:16: error: 'g' was not declared in this scope
../src/A.cpp: In destructor 'com::xxxx::test::A::~A()':
../src/A.cpp:20: error: 'g' was not declared in this scope
../src/A.cpp: In member function 'void com::xxxx::test::A::method1()':
../src/A.cpp:24: error: 'g' was not declared in this scope

What could be the mistake in the above code? 上面代码中的错误可能是什么?

Thank you in advance, 先感谢您,
Regards, 问候,
Raghava. Raghava。

The forward declaration 前瞻性声明

 class com::xxxx::test::G;

is illegal. 是非法的。 Members of a namespace must be declared within it. 必须在其中声明命名空间的成员。

namespace com { namespace xxxx { namespace test {
    class G;

Also, as Kenny says, namespaces aren't used like this in C++. 另外,正如Kenny所说,在C ++中没有像这样使用命名空间。 Unless your project is distributed as a library or is of reasonably large size (dozens of files minimum), you probably don't need your own namespace. 除非您的项目作为库分发或具有相当大的大小(最少几十个文件),否则您可能不需要自己的命名空间。

When the compiler tries to compile a.cop, the first thing it encounters (included from ah) is this: 当编译器尝试编译a.cop时,它遇到的第一件事(包含在啊)是这样的:

class com::xxxx::test::G;

At this point there is nothing to tell the compiler what exactly com, xxxx and test are. 在这一点上,没有什么可以告诉编译器究竟是什么com,xxxx和test。 Each of these can be either a namespace or a class. 这些中的每一个都可以是命名空间或类。 This also means that G is unclear, which leads to all other errors. 这也意味着G不清楚,这会导致所有其他错误。

Is class com::xxxx::test::G; class com::xxxx::test::G; legal in C++ ? 在C ++中合法吗? I would have written: 我会写:

namespace com {
   namespace xxxx {
       namespace test {
          class G;
       }
   }
}

As others have pointed out, using class com::xxxx::test::G; 正如其他人所指出的,使用class com::xxxx::test::G; is illegal. 是非法的。

The simpler conversion is (preserving inline-ness): 更简单的转换是(保留内联):

namespace com { namespace xxxx { namespace test { class G; } } }

I prefer this way of forward declaring because "grepping" does not show scope, whereas this syntax immediately lay it out for all to see. 我更喜欢这种前向声明的方式,因为“grepping”没有显示范围,而这种语法立即让所有人看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM