Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > FAQ Issue

Reply
Thread Tools

FAQ Issue

 
 
Loran Hayden
Guest
Posts: n/a
 
      02-10-2004
Hi Folks:

I've been working on my set theory project which I mentioned in a prior note
asking about design patterns. Since I am creating templates and I'll need to
do some reference counting, so I consulted the faq for some guidance.

http://www.parashift.com/c++-faq-lit...html#faq-16.21 shows
how to do reference counting so I decided I would simply try templating the
Fred class as follows:

(My additions to the previously compiling code are marked by // Modified
code) I've added two lines as you'll see (and omitted comments from the
original for posting here).


// Fred.h

class FredPtr;

template<typename t> // Modified code
class Fred {
public:
Fred() : count_(0){ }
private:
friend FredPtr;
unsigned count_;
t _t; // Modified
code
};

class FredPtr {
public:
Fred* operator-> () { return p_; }

Fred& operator* () { return *p_; }

FredPtr(Fred* p) : p_(p) { ++p_->count_; }

~FredPtr() { if (--p_->count_ == 0) delete p_; }

FredPtr(const FredPtr& p) : p_(p.p_) { ++p_->count_; }

FredPtr& operator= (const FredPtr& p)
{
++p.p_->count_;
if (--p_->count_ == 0) delete p_;
p_ = p.p_;
return *this;
}

private:
Fred* p_;
};

As mentioned above, the code compiled fine until I tried the template.
Originally I used this main,

#include "Fred.hpp"

int main()
{
Fred f;

return 0;
}

which I changed to

#include "Fred.hpp"

int main()
{
Fred<int> f;

return 0;
}

and I get the compiler errors....

--------------------Configuration: FredTest - Win32
Debug--------------------
Compiling...
FredTest.cpp
c:\program files\microsoft visual studio\myprojects\fred\fred.hpp(22) :
error C2248: 'count_' : cannot access private member declared in class
'Fred'
c:\program files\microsoft visual
studio\myprojects\fred\fred.hpp(12) : see declaration of 'count_'
c:\program files\microsoft visual studio\myprojects\fred\fred.hpp(23) :
error C2248: 'count_' : cannot access private member declared in class
'Fred'
c:\program files\microsoft visual
studio\myprojects\fred\fred.hpp(12) : see declaration of 'count_'
c:\program files\microsoft visual studio\myprojects\fred\fred.hpp(24) :
error C2248: 'count_' : cannot access private member declared in class
'Fred'
c:\program files\microsoft visual
studio\myprojects\fred\fred.hpp(12) : see declaration of 'count_'
c:\program files\microsoft visual studio\myprojects\fred\fred.hpp(2 :
error C2248: 'count_' : cannot access private member declared in class
'Fred'
c:\program files\microsoft visual
studio\myprojects\fred\fred.hpp(12) : see declaration of 'count_'
c:\program files\microsoft visual studio\myprojects\fred\fred.hpp(29) :
error C2248: 'count_' : cannot access private member declared in class
'Fred'
c:\program files\microsoft visual
studio\myprojects\fred\fred.hpp(12) : see declaration of 'count_'
Error executing cl.exe.

FredTest.exe - 5 error(s), 0 warning(s)

PS: I get the same error if I comment out the token template member _t in
the original Fred class but leave it decalred as a template (no surprise
there).

I'm using Dev Studio 6.0

any ideas?


 
Reply With Quote
 
 
 
 
Michael Mellor
Guest
Posts: n/a
 
      02-10-2004
Loran Hayden wrote:
> Hi Folks:
>
> I've been working on my set theory project which I mentioned in a prior note
> asking about design patterns. Since I am creating templates and I'll need to
> do some reference counting, so I consulted the faq for some guidance.
>
> http://www.parashift.com/c++-faq-lit...html#faq-16.21 shows
> how to do reference counting so I decided I would simply try templating the
> Fred class as follows:
>
> (My additions to the previously compiling code are marked by // Modified
> code) I've added two lines as you'll see (and omitted comments from the
> original for posting here).
>
>
> // Fred.h
>
> class FredPtr;
>
> template<typename t> // Modified code
> class Fred {
> public:
> Fred() : count_(0){ }
> private:
> friend FredPtr;


I do believe the word class is needed here (and therefore the FAQ has a
mistake).
friend class FredPtr;

> unsigned count_;
> t _t; // Modified
> code
> };
>
> class FredPtr {
> public:
> Fred* operator-> () { return p_; }


You have declared Fred as a template, yet here you are not telling
compiler what type t should be.

>
> Fred& operator* () { return *p_; }


and again.

>
> FredPtr(Fred* p) : p_(p) { ++p_->count_; }


once more

>
> ~FredPtr() { if (--p_->count_ == 0) delete p_; }
>
> FredPtr(const FredPtr& p) : p_(p.p_) { ++p_->count_; }
>
> FredPtr& operator= (const FredPtr& p)
> {
> ++p.p_->count_;
> if (--p_->count_ == 0) delete p_;
> p_ = p.p_;
> return *this;
> }
>
> private:
> Fred* p_;
> };
>

[....]

Mike
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      02-10-2004
Loran Hayden wrote in
news:%h6Wb.7757$(E-Mail Removed) ble.rogers.com:

[snip]
>


template < typename T >

> class FredPtr;
>
> template<typename t> // Modified code
> class Fred {
> public:
> Fred() : count_(0){ }
> private:
> friend FredPtr;


Your missing a "class" above, change too:

friend class FresPtr< T >;

> unsigned count_;
> t _t; //
> Modified
> code


P.S when posting to usenet use /* style */ coments they wrap better.

> };
>


template <typename T>

> class FredPtr {
> public:
> Fred* operator-> () { return p_; }


Should now be:

Fred< T > * operator-> () { return p_; }

etc ...

>
> Fred& operator* () { return *p_; }
>
> FredPtr(Fred* p) : p_(p) { ++p_->count_; }
>
> ~FredPtr() { if (--p_->count_ == 0) delete p_; }
>
> FredPtr(const FredPtr& p) : p_(p.p_) { ++p_->count_; }
>
> FredPtr& operator= (const FredPtr& p)
> {
> ++p.p_->count_;
> if (--p_->count_ == 0) delete p_;
> p_ = p.p_;
> return *this;
> }
>
> private:
> Fred* p_;
> };
>

[snip]

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Loran Hayden
Guest
Posts: n/a
 
      02-10-2004
Thanks a lot, people. Works fine now.
regards,
L.

"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns948B9AFF83671ukcoREMOVEfreenetrtw@195.129. 110.205...
> Loran Hayden wrote in
> news:%h6Wb.7757$(E-Mail Removed) ble.rogers.com:
>
> [snip]
> >

>
> template < typename T >
>
> > class FredPtr;
> >
> > template<typename t> // Modified code
> > class Fred {
> > public:
> > Fred() : count_(0){ }
> > private:
> > friend FredPtr;

>
> Your missing a "class" above, change too:
>
> friend class FresPtr< T >;
>
> > unsigned count_;
> > t _t; //
> > Modified
> > code

>
> P.S when posting to usenet use /* style */ coments they wrap better.
>
> > };
> >

>
> template <typename T>
>
> > class FredPtr {
> > public:
> > Fred* operator-> () { return p_; }

>
> Should now be:
>
> Fred< T > * operator-> () { return p_; }
>
> etc ...
>
> >
> > Fred& operator* () { return *p_; }
> >
> > FredPtr(Fred* p) : p_(p) { ++p_->count_; }
> >
> > ~FredPtr() { if (--p_->count_ == 0) delete p_; }
> >
> > FredPtr(const FredPtr& p) : p_(p.p_) { ++p_->count_; }
> >
> > FredPtr& operator= (const FredPtr& p)
> > {
> > ++p.p_->count_;
> > if (--p_->count_ == 0) delete p_;
> > p_ = p.p_;
> > return *this;
> > }
> >
> > private:
> > Fred* p_;
> > };
> >

> [snip]
>
> HTH.
>
> Rob.
> --
> http://www.victim-prime.dsl.pipex.com/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ or not FAQ? =?ISO-8859-15?Q?Juli=E1n?= Albo C++ 28 01-15-2007 04:33 AM
FAQ/FAQ notes site makeover Peter Michaux Javascript 22 11-27-2006 01:55 AM
FAQ - How do I direct someone to this FAQ? FAQ server Javascript 1 08-04-2006 10:13 PM
FAQ issue: Guaranteed value ranges of fundamental types? Alf P. Steinbach C++ 30 02-14-2005 09:46 AM
[de] Update of FAQ in German/FAQ auf Deutsch ueberarbeitet Josef 'Jupp' Schugt Ruby 0 09-22-2003 08:56 PM



Advertisments