compile error on constructor/private

Discussion in 'C++' started by eric, Aug 10, 2011.

  1. eric

    eric Guest

    Dear C/g++ experts:

    I declare the following
    --------------------------------------------------------
    template<typename T>
    class DOMPtr {
    public:
    DOMPtr(T* t) : t_(t) { }
    ~DOMPtr() { t_->release(); }
    T* operator->() const { return t_; }
    private:
    // prohibit copying and assigning
    DOMPtr(const DOMPtr&); // this is my line 38
    DOMPtr& operator=(const DOMPtr&);
    T* t_;
    };
    -------------------------------------------------------------------------------
    then use it here
    ----------------------------------------------------------
    DOMPtr<DOMBuilder> parser =
    static_cast<DOMImplementationLS*> // this is 94
    (impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
    -------------------
    // Construct a DOMWriter to save animals.xml
    DOMPtr<DOMWriter> writer =
    static_cast<DOMImplementationLS*>(impl)-
    >createDOMWriter(); // this 140

    -----------------------
    but it can not compile success
    --------------------
    Example14-10.cpp: In function ‘int main()’:
    Example14-10.cpp:38:7: error: ‘DOMPtr<T>::DOMPtr(const DOMPtr<T>&)
    [with T = xercesc_2_8::DOMBuilder, DOMPtr<T> =
    DOMPtr<xercesc_2_8::DOMBuilder>]’ is private
    Example14-10.cpp:94:111: error: within this context
    Example14-10.cpp:38:7: error: ‘DOMPtr<T>::DOMPtr(const DOMPtr<T>&)
    [with T = xercesc_2_8::DOMWriter, DOMPtr<T> =
    DOMPtr<xercesc_2_8::DOMWriter>]’ is private
    Example14-10.cpp:140:70: error: within this context
    -------------------------------------------------------------------------------------------------------------------------
    it looks my instantiate go through private as well as constructor(or
    not go through constructor at all),
    which is out of expectation
    need your help/hint/suggestion on how to fix it, and not deviate from
    origin textcontext of book author
    (c++ cookbook)(you can download the code from
    http://examples.oreilly.com/9780596007614/
    on 14-10.cpp, and animals.xml at 14-1.xml, to test by yourself
    thanks a lot in advance, Eric(g++4.5.2, linux2.6.38-10)
     
    eric, Aug 10, 2011
    #1
    1. Advertising

  2. eric

    red floyd Guest

    On 8/10/2011 2:13 PM, eric wrote:
    > Dear C/g++ experts:
    >
    > I declare the following
    > --------------------------------------------------------
    > template<typename T>
    > class DOMPtr {
    > public:
    > DOMPtr(T* t) : t_(t) { }
    > ~DOMPtr() { t_->release(); }
    > T* operator->() const { return t_; }
    > private:
    > // prohibit copying and assigning
    > DOMPtr(const DOMPtr&); // this is my line 38
    > DOMPtr& operator=(const DOMPtr&);
    > T* t_;
    > };
    > -------------------------------------------------------------------------------
    > then use it here
    > ----------------------------------------------------------
    > DOMPtr<DOMBuilder> parser =
    > static_cast<DOMImplementationLS*> // this is 94
    > (impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
    > -------------------
    > // Construct a DOMWriter to save animals.xml
    > DOMPtr<DOMWriter> writer =
    > static_cast<DOMImplementationLS*>(impl)-
    >> createDOMWriter(); // this 140


    This is conceptually equivalent to:

    DOMPtr<DOMWriter> writer =
    DOMPtr<DomWriter>(static_cast ...);

    The compiler elides the assignment, but it still needs to be
    visible. And your assignment operator is private.

    Try rewriting line 94 and 140 as direct constructor calls:

    DOMPtr<DOMBuilder> parser(static_cast ...); // 94
    DOMPtr<DOMWriter> writer(static_cast ...); // 140
     
    red floyd, Aug 10, 2011
    #2
    1. Advertising

  3. eric

    eric Guest

    On Aug 10, 3:00 pm, red floyd <> wrote:
    > On 8/10/2011 2:13 PM, eric wrote:
    >
    >
    >
    > > Dear C/g++ experts:

    >
    > > I declare the following
    > > --------------------------------------------------------
    > >      template<typename T>
    > >    class DOMPtr {
    > >    public:
    > >        DOMPtr(T* t) : t_(t) { }
    > >        ~DOMPtr() { t_->release(); }
    > >        T* operator->() const { return t_; }
    > >    private:
    > >        // prohibit copying and assigning
    > >        DOMPtr(const DOMPtr&);                    // this is my line 38
    > >        DOMPtr&  operator=(const DOMPtr&);
    > >        T* t_;
    > >    };
    > > -------------------------------------------------------------------------------
    > > then use it here
    > > ----------------------------------------------------------
    > >          DOMPtr<DOMBuilder>  parser =
    > >              static_cast<DOMImplementationLS*>        // this is 94
    > > (impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
    > > -------------------
    > >          // Construct a DOMWriter to save animals.xml
    > >          DOMPtr<DOMWriter>  writer =
    > >              static_cast<DOMImplementationLS*>(impl)-
    > >> createDOMWriter(); // this 140

    >
    > This is conceptually equivalent to:
    >
    >      DOMPtr<DOMWriter> writer =
    >          DOMPtr<DomWriter>(static_cast ...);
    >
    > The compiler elides the assignment, but it still needs to be
    > visible.  And your assignment operator is private.
    >
    > Try rewriting line 94 and 140 as direct constructor calls:
    >
    >       DOMPtr<DOMBuilder> parser(static_cast ...); // 94
    >       DOMPtr<DOMWriter> writer(static_cast ...);  // 140


    Thanks it work
     
    eric, Aug 10, 2011
    #3
    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. qazmlp
    Replies:
    19
    Views:
    812
    Daniel T.
    Feb 4, 2004
  2. DaveLessnau
    Replies:
    3
    Views:
    435
    Howard
    May 16, 2005
  3. Replies:
    4
    Views:
    4,149
    Alf P. Steinbach
    Mar 3, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    890
    Lionel B
    Mar 1, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,287
Loading...

Share This Page