FAQ Issue

L

Loran Hayden

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-lite/freestore-mgmt.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(28) :
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?
 
M

Michael Mellor

Loran said:
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-lite/freestore-mgmt.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
 
R

Rob Williscroft

Loran Hayden wrote in

[snip]
template said:
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 said:
unsigned count_;
t _t; //
Modified
code

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

template said:
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.
 
L

Loran Hayden

Thanks a lot, people. Works fine now.
regards,
L.

Rob Williscroft said:
Loran Hayden wrote in

[snip]
template said:
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 said:
unsigned count_;
t _t; //
Modified
code

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

template said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top