FAQ Issue

Discussion in 'C++' started by Loran Hayden, Feb 10, 2004.

  1. Loran Hayden

    Loran Hayden Guest

    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?
     
    Loran Hayden, Feb 10, 2004
    #1
    1. Advertising

  2. 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-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
     
    Michael Mellor, Feb 10, 2004
    #2
    1. Advertising

  3. Loran Hayden wrote in
    news:%h6Wb.7757$:

    [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/
     
    Rob Williscroft, Feb 10, 2004
    #3
  4. Loran Hayden

    Loran Hayden Guest

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

    "Rob Williscroft" <> wrote in message
    news:Xns948B9AFF83671ukcoREMOVEfreenetrtw@195.129.110.205...
    > Loran Hayden wrote in
    > news:%h6Wb.7757$:
    >
    > [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/
     
    Loran Hayden, Feb 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    30
    Views:
    895
    Dave Thompson
    Feb 14, 2005
  2. =?ISO-8859-15?Q?Juli=E1n?= Albo

    FAQ or not FAQ?

    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 8, 2007, in forum: C++
    Replies:
    28
    Views:
    946
    Grizlyk
    Jan 15, 2007
  3. Josef 'Jupp' Schugt
    Replies:
    0
    Views:
    254
    Josef 'Jupp' Schugt
    Sep 22, 2003
  4. FAQ server

    FAQ - How do I direct someone to this FAQ?

    FAQ server, Aug 4, 2006, in forum: Javascript
    Replies:
    1
    Views:
    198
    Dr John Stockton
    Aug 4, 2006
  5. Peter Michaux

    FAQ/FAQ notes site makeover

    Peter Michaux, Nov 22, 2006, in forum: Javascript
    Replies:
    22
    Views:
    405
    Randy Webb
    Nov 27, 2006
Loading...

Share This Page