functor object in template class

  • Thread starter Chandra Shekhar Kumar
  • Start date
C

Chandra Shekhar Kumar

introduce a public member function like getVal for getting the value i then call this
function in write_row2database
 
P

porschberg

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
 
R

Rob Williscroft

porschberg wrote in
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
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.
 
P

porschberg

Rob Williscroft said:
porschberg wrote in
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
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.
 
V

Victor Bazarov

porschberg said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top