functor object in template class

Discussion in 'C++' started by Chandra Shekhar Kumar, Jun 24, 2003.

  1. introduce a public member function like getVal for getting the value i then call this
    function in write_row2database
    Chandra Shekhar Kumar, Jun 24, 2003
    #1
    1. Advertising

  2. Chandra Shekhar Kumar

    porschberg Guest

    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
    porschberg, Jun 25, 2003
    #2
    1. Advertising

  3. porschberg wrote in
    news::

    > 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/
    Rob Williscroft, Jun 25, 2003
    #3
  4. Chandra Shekhar Kumar

    porschberg Guest

    Rob Williscroft <> wrote in message news:<Xns93A5543576473ukcoREMOVEfreenetrtw@195.129.110.201>...
    > porschberg wrote in
    > news::
    >
    > > 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.
    porschberg, Jun 25, 2003
    #4
  5. "porschberg" <> 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
    Victor Bazarov, Jun 25, 2003
    #5
  6. Chandra Shekhar Kumar

    porschberg Guest

    Victor thanks a lot! I understand and it works now.
    thomas
    "Victor Bazarov" <> wrote in message news:<>...
    > "porschberg" <> 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
    porschberg, Jun 26, 2003
    #6
    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. CoolPint
    Replies:
    3
    Views:
    699
    tom_usenet
    Dec 29, 2003
  2. jack
    Replies:
    3
    Views:
    391
    Victor Bazarov
    Feb 28, 2004
  3. Replies:
    8
    Views:
    398
  4. christopher diggins
    Replies:
    16
    Views:
    738
    Pete Becker
    May 4, 2005
  5. Replies:
    14
    Views:
    953
    Christof Warlich
    Jul 19, 2012
Loading...

Share This Page