 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael Hopkins Guest
|
Posted: Fri Oct 28, 2005 1:28 pm Post subject: valarray subclass value assignment |
|
|
Hi all
I have a subclass of valarray<T> thus
template <typename T>
class uo_val : public std::valarray<T>
{
public:
uo_val ( ) : std::valarray<T>() {}
uo_val (const int sz ) : std::valarray<T>( sz ) {}
uo_val (const int sz, const T fill ) : std::valarray<T>( fill, sz ) {}
//etc..
};
and I want to define an operator
uo_val& uo_val::operator=( const T & )
to assign all values to the supplied T using valarray's operator
valarray& valarray::operator=( const T & )
But neither (1) nor (2) below work and I'm not sure why.
uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::operator=( val );
}
Refuses to compile with:
error: invalid initialization of reference of type 'hr::uo_val<double>&'
from expression of type 'std::valarray<double>'
uo_val& operator=( const T & val ) { // (2)
*this = val;
return *this;
}
Compiles but gives runtime error signal 11 (SIGSEGV)
Could someone please enlighten me?
Many thanks - please CC to my email
Michael
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ 'touch the future'
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Oct 28, 2005 1:50 pm Post subject: Re: valarray subclass value assignment |
|
|
Michael Hopkins wrote:
| Quote: | But neither (1) nor (2) below work and I'm not sure why.
uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::operator=( val );
}
Refuses to compile with:
error: invalid initialization of reference of type 'hr::uo_val<double>&'
from expression of type 'std::valarray<double>'
|
You can't bind a reference to uo_val to an std::valarray<T>.
| Quote: | uo_val& operator=( const T & val ) { // (2)
*this = val;
return *this;
}
Compiles but gives runtime error signal 11 (SIGSEGV)
|
Well, this is a recursion. Your operator= will call itself over and over
again, usually until your stack is exhausted.
How about:
uo_val& operator=( const T & val )
{
std::valarray<T>::operator=( val );
return *this;
}
|
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Fri Oct 28, 2005 1:52 pm Post subject: Re: valarray subclass value assignment |
|
|
Michael Hopkins wrote:
| Quote: |
Hi all
I have a subclass of valarray<T> thus
template <typename T
class uo_val : public std::valarray
{
public:
uo_val ( ) : std::valarray
uo_val (const int sz ) : std::valarray<T>( sz ) {}
uo_val (const int sz, const T fill ) : std::valarray<T>( fill, sz ) {}
//etc..
};
and I want to define an operator
uo_val& uo_val::operator=( const T & )
to assign all values to the supplied T using valarray's operator
valarray& valarray::operator=( const T & )
But neither (1) nor (2) below work and I'm not sure why.
uo_val& operator=( const T & val ) { // (1)
return std::valarray<T>::operator=( val );
|
you are returning the subobject of type std::valarray<T>. Try:
uo_val & operator= ( T const & t ) {
std::valarray<T>::operator= ( t );
return( *this );
}
| Quote: | }
Refuses to compile with:
error: invalid initialization of reference of type 'hr::uo_val<double>&'
from expression of type 'std::valarray<double>'
uo_val& operator=( const T & val ) { // (2)
*this = val;
return *this;
}
Compiles but gives runtime error signal 11 (SIGSEGV)
|
You have an infinite loop. The segfault is most likely a stack overflow.
Best
Kai-Uwe Bux
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|