簡體   English   中英

類外的前向聲明有效,但嵌套時無效

[英]Forward declaration outside class works, but not when nested

假設我有兩個使用pimpl習語的AB類。 A提供公共API,持有指向B的指針。 A向前聲明B時會出現編譯錯誤,但在外部聲明時則不會出現編譯錯誤。

為什么后者不起作用? 在這兩種情況下,我a.cpp在調用B任何方法之前在a.cpp包含b.hpp

A級以外的前方聲明

此示例正常工作。

文件a.hpp

#ifndef _A_
#define _A_

#include <memory>

class B; // forward declaration, defined in a.cpp
class A {
public:
        A();
        ~A();
        void Hi();
private:
        std::unique_ptr< B > b_;
};

#endif

文件a.cpp

#include "b.hpp"
#include "a.hpp"

A::A() : b_( std::make_unique< B >() ) { }
A::~A() { }

void
A::Hi() {
        this->b_->Hi();
}

文件b.hpp

#ifndef _B_
#define _B_

class B {
public:
        void Hi();
};

#endif

文件b.cpp

#include "b.hpp"

#include <iostream>

void
B::Hi() {
        std::cout << "Hello World!" << std::endl;
}

驅動程序文件hello.cpp

#include "a.hpp"

int main() {
        A a;
        a.Hi();
        return 0;
}

編譯: g++ hello.cpp a.cpp b.cpp -std=c++14

A類內的前向聲明

在這里,我感動向前聲明BA

文件a.hpp

#ifndef _A_
#define _A_

#include <memory>

class A {
public:
        A();
        ~A();
        void Hi();
private:
        class B; // forward declaration, defined in a.cpp
        std::unique_ptr< B > b_;
};

#endif

我收到以下編譯錯誤:

a.cpp: In member function ‘void A::Hi()’:
a.cpp:9:10: error: invalid use of incomplete type ‘class A::B’
  this->b_->Hi();
          ^
In file included from a.cpp:2:0:
a.hpp:12:8: error: forward declaration of ‘class A::B’
  class B;
        ^
In file included from /usr/include/c++/4.9/memory:81:0,
                 from a.hpp:4,
                 from a.cpp:2:
/usr/include/c++/4.9/bits/unique_ptr.h: In instantiation of ‘typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = A::B; _Args = {}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<A::B>]’:
a.cpp:4:36:   required from here
/usr/include/c++/4.9/bits/unique_ptr.h:765:69: error: invalid use of incomplete type ‘class A::B’
     { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
                                                                     ^
In file included from a.cpp:2:0:
a.hpp:12:8: error: forward declaration of ‘class A::B’
  class B;
        ^
In file included from /usr/include/c++/4.9/memory:81:0,
                 from a.hpp:4,
                 from a.cpp:2:
/usr/include/c++/4.9/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = A::B]’:
/usr/include/c++/4.9/bits/unique_ptr.h:236:16:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = A::B; _Dp = std::default_delete<A::B>]’
a.cpp:4:36:   required from here
/usr/include/c++/4.9/bits/unique_ptr.h:74:22: error: invalid application of ‘sizeof’ to incomplete type ‘A::B’
  static_assert(sizeof(_Tp)>0,

這是因為你沒有聲明同一個class B 第二個示例中的B被聲明為嵌套類 它的范圍是A:: :(因為它實際上命名為A::BA::B )。 但是你嘗試將它用作::B (如在全局范圍內)。

從錯誤消息中可以看出這一點:

錯誤:無效使用不完整類型' A類:: B '

這不行。 A外部聲明class B是正確的實現方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM