I am currently doing a lot on exception security. (Extraordinary C ++, C ++ FAQ, Light of Herb Sitter, etc.)
Specifically, I want to write and understand the reference count example, but I am currently in this part of the code I'm stuck on:
Class Smart_Ptr; Square Fu {Private: Friend Class Smart_Ptr; Unsigned int count_; Public: Build Smart_Ptr () // Here error {New Fu (Return) (); } Private: Fu (): count_ (0) {} / * ... * / / * Smart_Ptr class started here * /
As you can see, I am trying to force the user to make local variables of my foo object. I basically, this is what is written in C ++ FAQ; I have just changed Fred to Fu and Fredprint into Smart_Ptr, which I realized was a mistake since the connection is hard to see now.
My compiler exited the error:
Error C2027: Use of undefined type 'Smart_Ptr'
I'm pretty sure That's why Smart_Ptr is completely defined and the FAQ is a complete duplicate of the code. I have copied the code altogether, as is, and received the same error.
Last important part of the question
Since I'm thinking incorrectly, I post 'substantial' of the source code to debug the problem , And I terminate it incorrectly, I'll post the rest of the code here.
/ * Foo.h * / square Smart_Ptr; Square Fu {Private: Friend Class Smart_Ptr; Unsigned int count_; Public: Create SmartPtr () return (return new FU ()); } Private: Foo (): count_ (0) {}}; Class Smart_Ptr {Private: Foo * p_; // p_ Never tap is public: Foo * operator-> () {Return p_; } Foo & amp; amp; Operator * () {return * p_; } Smart_Ptr (Foo * p): p_ (p) {++ p _- & gt; Count_; } ~ Smart_Ptr () {if (--p _- & gt; count_ == 0) delete p_; } Smart_Ptr (Smart_Ptr const & amp; P): p_ (p.p_) {++ p _- & gt; Count_; } Smart_Ptr & amp; Operator = (Smart_Ptr const & amp; P) {Foo * const old = p_; P_ = p.p_; ++ P_- & gt; Count_; If (- ---> count == 0) delete old; Return * This; }};
You can not write a task that is Smart_Ptr
Gives by value, unless a Smart_Ptr
is defined, a forward declaration is not enough.
The link you link to has a comment in it, // defined below category FredPtr {...};
, but you FU
class in the code definition> Create function. If you look closely at the code after "Changes to Fred's cloud", you will see that create
is declared in the class only: it is later defined by the following code:
Inline FredPtr Fred :: Create () {New Fred (back); }
You only have to do that.
Comments
Post a Comment