C++ 在C++;,Can';我是否在不分离头文件和cpp的情况下实现这些类?

C++ 在C++;,Can';我是否在不分离头文件和cpp的情况下实现这些类?,c++,class,C++,Class,假设我有“A.hpp”、“B.hpp”和“main.cpp” A.hpp 水电站 main.cpp 我知道为什么我不能编译main.cpp,但不知道如何在不分离类A和B的头文件和源文件的情况下修复这种情况(例如,类A和B使用模板) 提前感谢。:) 如果我理解正确-您希望能够编译main.cpp,而不需要对A和B使用单独的翻译单元,也不需要分离A和B的接口和实现 您可以这样做,但您仍需要遵循转发声明的规则: class B; // class `B` forward-declaration //

假设我有“A.hpp”、“B.hpp”和“main.cpp”

A.hpp 水电站 main.cpp 我知道为什么我不能编译main.cpp,但不知道如何在不分离类A和B的头文件和源文件的情况下修复这种情况(例如,类A和B使用模板)


提前感谢。:)

如果我理解正确-您希望能够编译
main.cpp
,而不需要对
A
B
使用单独的翻译单元,也不需要分离
A
B
的接口和实现

您可以这样做,但您仍需要遵循转发声明的规则:

class B; // class `B` forward-declaration

// define class A, but don't implement the parts that need B's definition

class A {
public:
    B& b_; // `A` can use `B` here, but the compiler still doesn't know how B is defined

    A(B&& b); // also need to define A's structure, but not the method implementations
    void foo(); 
};

class B {
public:
    A* a_;
    B() : {
        a_ = new A( *this );
    }
    void foo() { }
};

// now we can define `A` using the `A::` syntax, instead of class{}:
A::A(B&& b) : b_(b) { }
void A::foo() { b_.foo(); }

int main()
{
   B b;
   b.a->foo();
   return 0;
}

如果我理解正确-您希望能够编译
main.cpp
,而不需要对
A
B
使用单独的翻译单元,也不需要分离
A
B
的接口和实现

您可以这样做,但您仍需要遵循转发声明的规则:

class B; // class `B` forward-declaration

// define class A, but don't implement the parts that need B's definition

class A {
public:
    B& b_; // `A` can use `B` here, but the compiler still doesn't know how B is defined

    A(B&& b); // also need to define A's structure, but not the method implementations
    void foo(); 
};

class B {
public:
    A* a_;
    B() : {
        a_ = new A( *this );
    }
    void foo() { }
};

// now we can define `A` using the `A::` syntax, instead of class{}:
A::A(B&& b) : b_(b) { }
void A::foo() { b_.foo(); }

int main()
{
   B b;
   b.a->foo();
   return 0;
}


你为什么不想分开?在您的情况下,似乎需要分离。确保您可以在没有头文件的情况下执行任何操作,只需将所有内容放在单个文件中即可。由于这是
#include
为您所做的。@appleapple在
a
-to-
B
中有一个循环引用,有一个包含防护装置,因此编译器会抱怨
a.hpp
(通过
B.hpp
通过
main.cpp
包含)引用了未定义的符号
B
。@Dai是的,无论如何,我错过了这个,分隔文件与否无关。这不是问题所在,但以下划线开头,后跟大写字母(
\u a\u CPP\u
)的名称和包含两个连续下划线的名称保留供实现使用。不要在代码中使用它们。为什么不分离?在您的情况下,似乎需要分离。确保您可以在没有头文件的情况下执行任何操作,只需将所有内容放在单个文件中即可。由于这是
#include
为您所做的。@appleapple在
a
-to-
B
中有一个循环引用,有一个包含防护装置,因此编译器会抱怨
a.hpp
(通过
B.hpp
通过
main.cpp
包含)引用了未定义的符号
B
。@Dai是的,无论如何,我错过了这个,分隔文件与否无关。这不是问题所在,但以下划线开头,后跟大写字母(
\u a\u CPP\u
)的名称和包含两个连续下划线的名称保留供实现使用。不要在代码中使用它们。在其
块中缺少
A
方法的声明。@MatteoItalia谢谢-我忘了这一点。此外,如果您希望这些内容位于标题中,则需要将它们显式标记为
内联
,否则,链接时会出现多个定义错误。@Matteo。请注意,如果成员函数定义位于类定义内,则它们是隐式内联的。因此,仅当成员函数定义被放置在类定义之外时才需要内联,如本答案中所示。您在其
块中缺少
A
方法的声明。@MatteoItalia谢谢您-我也忘了,如果您想将这些内容放在头中,它们需要显式标记为
inline
,否则链接时会出现多个定义错误。@Matteo确实如此。请注意,如果成员函数定义位于类定义内,则它们是隐式内联的。因此,只有当成员函数定义放置在类定义之外时,才需要内联,就像本答案中的情况一样。
#include "B.hpp"
#include "A.hpp"

int main()
{
   B b;
   b.a->foo();
   return 0;
}
class B; // class `B` forward-declaration

// define class A, but don't implement the parts that need B's definition

class A {
public:
    B& b_; // `A` can use `B` here, but the compiler still doesn't know how B is defined

    A(B&& b); // also need to define A's structure, but not the method implementations
    void foo(); 
};

class B {
public:
    A* a_;
    B() : {
        a_ = new A( *this );
    }
    void foo() { }
};

// now we can define `A` using the `A::` syntax, instead of class{}:
A::A(B&& b) : b_(b) { }
void A::foo() { b_.foo(); }

int main()
{
   B b;
   b.a->foo();
   return 0;
}