473,386 Members | 2,129 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,386 software developers and data experts.

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
Jul 19 '05 #1
5 5131
introduce a public member function like getVal for getting the value i then call this
function in write_row2database

Jul 19 '05 #2
porschberg wrote in
news:8d**************************@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/
Jul 19 '05 #3
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.201>...
porschberg wrote in
news:8d**************************@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.
Jul 19 '05 #4
"porschberg" <th***************@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
Jul 19 '05 #5
Victor thanks a lot! I understand and it works now.
thomas
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<vf************@corp.supernews.com>...
"porschberg" <th***************@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

Jul 19 '05 #6

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

Similar topics

6
by: Gert Van den Eynde | last post by:
Hi all, I'm struggling a bit with Functors generated for an ABC. This is the functor code: class Functor{ public: virtual double operator(double x)=0 }
8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
3
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can...
8
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
3
by: alan | last post by:
Hello all, I'd like to know if there is a nice method of defining a functor creator which accepts an N-ary function and returns a functor based on that function. For example I have a function:...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.