CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2007
    Posts
    3

    STL derived function not found?

    I am trying to write a few functions to extend the STL vector class. What I have below seems to work fine for built-in types like "double," "int," what have you, but produces an error when I use abstract types like complex<double>.

    Here's my code:

    Code:
    #include <complex>
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    template <class D>
    class statistics_vector : public vector<D> {
    public:
      D sum( void );
      D mean( void );
    };
    
    template <class D>
    D statistics_vector<D>::sum( void ) {
      typename vector<D>::const_iterator position;
      D arithmetic_sum = 0;
    
      for( position = this->begin(); position != this->end(); ++position )
        arithmetic_sum += *position;
    
      return arithmetic_sum;
    }
    
    template <class D>
    D statistics_vector<D>::mean( void ) {
      return sum() / vector<D>::size();
    }
    
    int main( void ) {
      statistics_vector<complex<double> > values;
      complex<double> a( 1, 1 ), b( 2, 2 ), c( 3, 3 ), mean_value;
      
      values.push_back( a );
      values.push_back( b );
      values.push_back( c );
    
      mean_value = values.mean();
       
      return EXIT_SUCCESS;
    }
    And here's the error message:

    Code:
    statistics_vector.cc: In member function `D statistics_vector<D>::mean() [with D = std::complex<double>]':
    statistics_vector.cc:39:   instantiated from here
    statistics_vector.cc:28: error: no match for 'operator/' in '((statistics_vector<std::complex<double> >*)this)->statistics_vector<D>::sum [with D = std::complex<double>]() / ((std::vector<std::complex<double>, std::allocator<std::complex<double> > >*)this)->std::vector<_Tp, _Alloc>::size [with _Tp = std::complex<double>, _Alloc = std::allocator<std::complex<double> >]()'

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: STL derived function not found?

    Quote Originally Posted by jhwilliams View Post
    I am trying to write a few functions to extend the STL vector class.
    Deriving from an STL container class is not recommended. The class was not intended to be derived from, as there is a lack of a virtual destructor. The design goal of std::vector and other containers is to use them within your own objects, i.e. aggregation, not inherit from them.

    Second, what if later on, you want to do a "sum" or "mean" on a container of std:eque? Or a std::list? Or maybe just a plain old array? You have to rewrite the code again for these scenarios if you want this functionality, given you are deriving from std::vector. The concept of iterators is what is used in this case, not derivation.

    As for the error, does these operations work with the types you're using? You don't need vector to determine this. Just write a tiny program, and apply those operations you're attempting to do with the types you're using. If it doesn't compile, then that is the reason for the error -- those types do not support those operations.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured