![]() |
Re: functor object in template class
introduce a public member function like getVal for getting the value i then call this
function in write_row2database |
functor object in template class
Hi,
I have a template class that looks like: template <class T> class Update { friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename T::row>&); friend ostream& operator<< (ostream&, const shared_ptr<typename T::row>&); friend struct write_row2database; public: typedef shared_ptr<typename T::row> ROW; typedef typename vector<ROW>::iterator iter; typedef typename vector<ROW>::value_type val; .... struct display_row { void operator()(val& aVal) { cout << aVal; } }; struct write_row2database { void operator()(val& aVal) { // (Update<T>).i << aVal; // !!!!! the problem !!!!! // i is (private) member of template class, how to address ? } .... bool flush(string& error) { for_each(v.begin(), v.end(), display_row()); //works for_each(v.begin(), v.end(), write_row2database()); } private: ... vector<ROW> v; otl_nocommit_stream i; ... }; My question: How can I write to i inside write_row2database , what is the correct syntax? I declared friend struct write_row2database; and there exist a operator overloading for "otl_stream<<theROW". Looking wishful forward to a reply, Thomas |
Re: functor object in template class
porschberg wrote in
news:8d9566f5.0306242159.619d665e@posting.google.c om: > Hi, > I have a template class that looks like: > > template <class T> > class Update > { > friend otl_stream& operator<< > (otl_stream&, const shared_ptr<typename T::row>&); > friend ostream& operator<< > (ostream&,const shared_ptr<typename T::row>&); // insert this: struct write_row2database; > friend struct write_row2database; /* without the inserted forward declation the write_row2database here referes to ::write_row2database not Update<T>::write_row2database */ >public: [snip] > struct write_row2database { > void operator()(val& aVal) { > // (Update<T>).i << aVal; > // !!!!! the problem !!!!! > // i is (private) member of template class, how to address ? > } > [snip] HTH Rob. -- http://www.victim-prime.dsl.pipex.com/ |
Re: functor object in template class
Rob Williscroft <rtw@freenet.REMOVE.co.uk> wrote in message news:<Xns93A5543576473ukcoREMOVEfreenetrtw@195.129 .110.201>...
> porschberg wrote in > news:8d9566f5.0306242159.619d665e@posting.google.c om: > > > Hi, > > I have a template class that looks like: > > > > template <class T> > > class Update > > { > > friend otl_stream& operator<< > > (otl_stream&, const shared_ptr<typename T::row>&); > > friend ostream& operator<< > > (ostream&,const shared_ptr<typename T::row>&); > > // insert this: > > struct write_row2database; > > > friend struct write_row2database; > > /* without the inserted forward declation the > write_row2database here referes to ::write_row2database > not Update<T>::write_row2database > */ > > >public: > [snip] > > struct write_row2database { > > void operator()(val& aVal) { > > // (Update<T>).i << aVal; > > // !!!!! the problem !!!!! > > // i is (private) member of template class, how to address ? > > } > > > [snip] > > HTH > > Rob. Hi Rob, the forward declaration did not help me. I think the line (Update<T>).i << aVal; is wrong. compiler says: Update.hh:101: error: parse error before `;' token If I change the line to i << aVal; I get: Update.hh:101: error: 'struct Update<P_WHReasonUpdate>::write_row2database' has no member named 'i' what I understand. My aim was to replace the handwritten loop with the for_each algorithm but now I see no way to foist this i to the function object. |
Re: functor object in template class
"porschberg" <thomas.porschberg@osp-dd.de> wrote...
> Hi, > I have a template class that looks like: > > template <class T> > class Update > { > friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename T::row>&); > friend ostream& operator<< (ostream&, const shared_ptr<typename T::row>&); > friend struct write_row2database; > public: > typedef shared_ptr<typename T::row> ROW; > typedef typename vector<ROW>::iterator iter; > typedef typename vector<ROW>::value_type val; > > ... > > struct display_row { > void operator()(val& aVal) { > cout << aVal; > } > }; > > struct write_row2database { > void operator()(val& aVal) { > // (Update<T>).i << aVal; > // !!!!! the problem !!!!! > // i is (private) member of template class, how to address ? > } }; Hereby lies the problem. Class 'write_row2database', though declared nested to "Update" template, knows NOTHING about the Update object to which you attempt to output the 'aVal'. What you need to do is to create 'write_row2database' so that it know what 'Update' object to use: struct write_row2database { Update& upd; write_row2database(Update& u) : upd(u) {} void operator()(val& aVal) { upd.i << aVal; } }; > > ... > bool flush(string& error) > { > for_each(v.begin(), v.end(), display_row()); //works > for_each(v.begin(), v.end(), write_row2database()); And here you HAVE to pass the Update object for which the functor is created: for_each(v.begin(), v.end(), write_row2database(*this)); > } > > private: > ... > vector<ROW> v; > otl_nocommit_stream i; > ... > > }; > > My question: > How can I write to i inside write_row2database , > what is the correct syntax? > I declared > friend struct write_row2database; > and there exist a operator overloading for "otl_stream<<theROW". Victor |
Re: functor object in template class
Victor thanks a lot! I understand and it works now.
thomas "Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message news:<vfjccbadca038b@corp.supernews.com>... > "porschberg" <thomas.porschberg@osp-dd.de> wrote... > > Hi, > > I have a template class that looks like: > > > > template <class T> > > class Update > > { > > friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename > T::row>&); > > friend ostream& operator<< (ostream&, const shared_ptr<typename > T::row>&); > > friend struct write_row2database; > > public: > > typedef shared_ptr<typename T::row> ROW; > > typedef typename vector<ROW>::iterator iter; > > typedef typename vector<ROW>::value_type val; > > > > ... > > > > struct display_row { > > void operator()(val& aVal) { > > cout << aVal; > > } > > }; > > > > struct write_row2database { > > void operator()(val& aVal) { > > // (Update<T>).i << aVal; > > // !!!!! the problem !!!!! > > // i is (private) member of template class, how to address ? > > } > > }; > > Hereby lies the problem. Class 'write_row2database', though declared > nested to "Update" template, knows NOTHING about the Update object > to which you attempt to output the 'aVal'. What you need to do is > to create 'write_row2database' so that it know what 'Update' object > to use: > > struct write_row2database { > Update& upd; > write_row2database(Update& u) : upd(u) {} > void operator()(val& aVal) { > upd.i << aVal; > } > }; > > > > > ... > > bool flush(string& error) > > { > > for_each(v.begin(), v.end(), display_row()); //works > > for_each(v.begin(), v.end(), write_row2database()); > > And here you HAVE to pass the Update object for which the functor > is created: > > for_each(v.begin(), v.end(), write_row2database(*this)); > > > } > > > > private: > > ... > > vector<ROW> v; > > otl_nocommit_stream i; > > ... > > > > }; > > > > My question: > > How can I write to i inside write_row2database , > > what is the correct syntax? > > I declared > > friend struct write_row2database; > > and there exist a operator overloading for "otl_stream<<theROW". > > Victor |
| All times are GMT. The time now is 05:35 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.