member function reference when extending an STL class

Discussion in 'C++' started by Alan, Jun 25, 2005.

  1. Alan

    Alan Guest

    I have a class, "event_list", derived from the template class "vector."
    I extended it with a method, "write" for this new class. Here's its
    definition:

    class event_list : public vector<Event>
    {
    public:
    void write (ofstream &filename, format_type format, int start, int
    end);
    };

    For what it`s worth, the class "Event" is defined thus:

    class Event
    {
    public:
    int date;
    string name;
    string poc;
    string type;
    Event();
    ~Event();
    void write(ofstream &filename, int start, int end);
    };

    In the "write" method for "event_list" (derived from vector), I want to
    go through every element and write it out. My problem is how to
    reference vector methods and operators within this method for the
    derived class. I currently have the following code, which will not
    compile:

    void write (ofstream &filename, format_type format, int start, int end)

    {
    // write out vector contents
    for (int indx = 0; indx < *this.size(); indx++)
    {
    current_date = *this[indx].date;
    if (current_date != last_date)
    {
    last_date = current_date;
    }
    *this[indx].write(filename, start, end);
    .. . .
    };

    However, the compiler is telling me that this is and invalid use of
    "this" in non-member function (all 3 uses of "this"). I thought this
    was the way to do it, since .size() is a function of the STL container
    class vector, and "event_list" should inherit this member function.

    Could anyone point out what I am doing wrong?
     
    Alan, Jun 25, 2005
    #1
    1. Advertising

  2. Alan wrote:
    > [...]
    > void write (ofstream &filename, format_type format, int start, int
    > end)
    >
    > {
    > // write out vector contents
    > for (int indx = 0; indx < *this.size(); indx++)
    > {
    > current_date = *this[indx].date;


    current_data = (*this)[indx].date;


    > if (current_date != last_date)
    > {
    > last_date = current_date;
    > }
    > *this[indx].write(filename, start, end);


    (*this)[indx].write(filename, start, end);

    > . . .
    > };


    V
     
    Victor Bazarov, Jun 25, 2005
    #2
    1. Advertising

  3. Alan

    Kai-Uwe Bux Guest

    Alan wrote:

    > I have a class, "event_list", derived from the template class "vector."
    > I extended it with a method, "write" for this new class. Here's its
    > definition:
    >
    > class event_list : public vector<Event>
    > {
    > public:
    > void write (ofstream &filename, format_type format, int start, int
    > end);
    > };
    >

    [snip]
    >
    > In the "write" method for "event_list" (derived from vector), I want to
    > go through every element and write it out. My problem is how to
    > reference vector methods and operators within this method for the
    > derived class. I currently have the following code, which will not
    > compile:
    >

    [snip]
    >
    > Could anyone point out what I am doing wrong?


    a) Some people frown upon deriving from std::vector. Note that the
    destructor of std::vector is not virtual. Thus, using a pointer
    to std::vector polymorphically will usually not work as intended.
    Beware of that.

    b) From your code, it is not clear at all why write() should be a method
    and not a static function that takes a stream and a std::vector<Event>
    as arguments.

    c) As for your problem, have a look at the following code. In
    particular, note the use of parentheses in write_alt(). As
    a matter of design, however, I would prefer the method write():
    you could replace std::vector by std::list and the code would
    still work. Thus, write() is more generic than write_alt().

    #include <iostream>
    #include <vector>

    class IntList : public std::vector< int > {
    public:

    void write ( std::eek:stream & ostr ) const {
    for ( IntList::const_iterator iter = this->begin();
    iter != this->end(); ++iter ) {
    ostr << *iter << ' ';
    }
    }

    void write_alt ( std::eek:stream & ostr ) const {
    for ( IntList::size_type index = 0;
    index < (*this).size(); ++ index ) {
    ostr << (*this)[index] << ' ';
    }
    }

    }; // class IntList

    int main ( void ) {
    IntList i_list;
    for ( int i = 0; i < 10; ++i ) {
    i_list.push_back( i );
    }
    i_list.write( std::cout );
    std::cout << '\n';
    i_list.write_alt( std::cout );
    }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 25, 2005
    #3
  4. Alan

    Alan Guest

    I tired putting parentheses around "*this", but that did not work
    either. Same error.
     
    Alan, Jun 25, 2005
    #4
  5. Alan wrote:
    > I tired putting parentheses around "*this", but that did not work
    > either. Same error.


    #include <iostream>
    #include <vector>

    using namespace std; // never do this in a public place

    class event_list : public vector<int>
    {
    public:
    void write(ostream& os) const
    {
    for (int i = 0; i < size(); i++)
    os << (*this) << ' '; // ****************
    os << endl;
    }
    };

    int main()
    {
    event_list el;
    el.push_back(1);
    el.push_back(2);
    el.push_back(4);

    el.write(cout);
    }


    Seems to work just fine...

    V
     
    Victor Bazarov, Jun 25, 2005
    #5
    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. E11
    Replies:
    1
    Views:
    4,937
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    12
    Views:
    737
    fluden
    Feb 11, 2005
  3. gerg
    Replies:
    1
    Views:
    334
    Bob Hairgrove
    Jan 14, 2006
  4. Jim Bancroft
    Replies:
    5
    Views:
    1,222
    Daniel T.
    Feb 13, 2008
  5. Praetorian
    Replies:
    11
    Views:
    2,383
    James Kanze
    Apr 3, 2008
Loading...

Share This Page