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?
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?