473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FAQ Issue

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(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?
Jul 22 '05 #1
3 1282
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
Jul 22 '05 #2
Loran Hayden wrote in
news:%h*****************@twister01.bloor.is.net.ca 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/
Jul 22 '05 #3
Thanks a lot, people. Works fine now.
regards,
L.

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.205...
Loran Hayden wrote in
news:%h*****************@twister01.bloor.is.net.ca 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/

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Mateer | last post by:
Hi, I have been running some queries against a table in a my database and have noted an odd (at least it seems odd to me) performance issue. The table has approximately 5 million rows and...
7
by: George Hester | last post by:
Please take a look at this google artcle: http://groups.google.com/groups?hl=en&lr=&frame=right&th=55d6f4b50f5f9382&seekm=411f370d%241%40olaf.komtel.net#link9 The op was having trouble with...
2
by: Anthony Cuttitta Jr. | last post by:
We have an application that outputs several different graphs from data downloaded from our AS400. The application has worked without (this) issue for several months now, but just recently, the...
0
by: Kevin Spencer | last post by:
Hi all, I am working on a service that uploads METAR weather information to the National Weather Service FTP site. The service I'm authoring is hosted on a Windows 200 server, and the NWS FTP...
2
by: Ben Rush | last post by:
Hello World, Okay, I have spent the day browsing the newsgroups and reading up on article after article concerning ViewState corruption and so forth, and I have a couple questions. We...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
0
by: Charles Leonard | last post by:
I am having yet another issue with Windows Server 2003. This time, the web service (a file import web service) appears to run except for one odd message: "ActiveX component can't create object". ...
4
by: Paul | last post by:
Hi, I've been struggling with this today, I'm developing a DotNet2.0 website in C# that needs to call a long running data query. Obviously this is a good candidate for an Asynchronous call, so...
1
by: AlekseyUS | last post by:
Hi, I'm a little stuck, I basically need to copy all the information within a specific file in Temp and append it to a file in another location. I'm not having any problems with smaller size...
13
by: SAL | last post by:
Hello, I'm trying to include a popup in the ItemTemplate of a gridview row. The ItemTemplate for the field contains a textbox and when the user clicks in the textbox I want a popup panel to show...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.