slice_iter and valarray

B

Boris Sargos

Hi,

I wrote the Matrix class suggested by Stroustrup in his reference book, and
in this aim, wrote too the following member function, which returns the ith
row of the Matrix :

std::valarray<double> MyMatrix::Row ( int i) const {
return ( ValArarray [slice ( (i-1) * nbCols, nbCols,
1) ] ) ;
}

where ValArray is of type std::valarray<double>.

No problem with the compilation, but while running, it crashes when the
destructor of the valarray is called. I don't understand

1) why it crashes
2) why Stroustrup advises to return a Slice_iter object instead of a
valarray ?


Mathematically, MyMatrix::Row(int) returns a vector. How would I create a
class MyVector compatible with this function MyMatrix::Row(int) ?


Thanks,
Boris.
 
E

E. Robert Tisdale

Boris said:
I wrote the Matrix class suggested by Stroustrup
in his reference book, and in this aim,
wrote too the following member function,
which returns the i'th row of the Matrix:

std::valarray<double> MyMatrix::Row(int i) const {
return ValArarray[slice((i-1) * nbCols, nbCols, 1)];
}

where ValArray is of type std::valarray<double>.

No problem with the compilation but, while running,
it crashes when the destructor of the valarray is called.
I don't understand

1) why it crashes
2) why Stroustrup advises to return a Slice_iter object
instead of a valarray ?

Mathematically, MyMatrix::Row(int) returns a vector.
How would I create a class MyVector
compatible with this function MyMatrix::Row(int)?

Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/

Also, visit
The Object-Oriented Numerics Page

http://www.oonumerics.org/oon/
 
S

Sebastian Thiebes

I included a valarray in my code and when trying to compile, Visual
Studio 6.0 gave these errors:

c:\program files\microsoft visual studio\vc98\include\valarray(104) :
warning
C4003: not enough actual parameters for macro 'min'
c:\program files\microsoft visual studio\vc98\include\valarray(110) :
warning
C4003: not enough actual parameters for macro 'max'

followed by 30 more, all in the code for valarray. After some advice and
googling I found out that the problems stems from the fact that in
windef.h there are two macros defined named min and max.

Now in a message to this group from 2002 the solution offered was to put
#undef min and #undef max just before including valarry. While this
works I also found another solution: One can disable those
Windows-specific macros with the preprocessor define NOMINMAX, as is
also described in Microsoft Knowledge Base Article - 143208,
http://support.microsoft.com/default.aspx?scid=kb;en-us;143208

I hope this is not too trivial a problem but I wanted to spare others
the hour or so I spent looking for a solution to this problem.

Best regards,

Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top