473,503 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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'

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Oct 28 '05 #1
2 2034
Michael Hopkins wrote:
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>.
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;
}

Oct 28 '05 #2
Michael Hopkins wrote:


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 );
you are returning the subobject of type std::valarray<T>. Try:

uo_val & operator= ( T const & t ) {
std::valarray<T>::operator= ( t );
return( *this );
}

}

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
Oct 28 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1707
by: Prune Tracy | last post by:
I have a question about assignment of slices of valarrays. I want to assign one general slice of a valarray to another. It seems the only way is to use the static_cast syntax, but this seems to...
6
4316
by: Steven T. Hatton | last post by:
I bought Josuttis's book on the repeated recommendations of people in this newsgroup. http://www.josuttis.com/libbook/ One of the first things I looked up was the std::valarray<>. And what I...
1
1335
by: Steven T. Hatton | last post by:
Examine the following code before you compile and run it. What do you expect to happen? Is what you expected consistent with the result of running it? #include <valarray> #include <iostream> ...
1
2061
by: Oliver Block | last post by:
I have a (9x9) valarray std::valarray<int> va(81); va = 0; // fil all elements with zeros I am taking row slices like that: std::slice_array<int> slr = va; // r = row; a value between 1...
2
2389
by: Jan Callewaert | last post by:
Hi, since I want to specify an extra function for a std::valarray<float>, I want to subclass it: class FVector : public std::valarray<float> { public: FVector() : std::valarray<float>() {}...
9
2128
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
5
2604
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
2
1936
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
43
4781
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.