compile error on constructor/private


E

eric

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 said:
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)
 
Ad

Advertisements

R

red floyd

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_;
};

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
 
Ad

Advertisements

E

eric

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
 

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

Top