G
Gerard Kramer
Presently I have some difficulties with the following type of code which
is understandably causing a segfault:
//---- begin code ----
#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {
private:
valarray <double> mx;
public:
SomeClass(valarray <double>& x) {mx = x;}
void func() {cout << mx[1];} // <---- Segfault here.
};
int main () {
valarray <double> x(5,10);
SomeClass foo(x);
foo.func();
return 0;
}
//---- end code ----
It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.
Yet (for a simple numerical code) I would like something similar to the
above to work, i.e. I'd like to pass a valarray of predefined size to
the constructor of a class to initialize a class-member valarray, say
mx, and after reference to mx's elements as mx[0], mx[1] etc. As a C++
starter I'm confused about the best way to work around the problem in
the code above. Any advice would be welcome.
Regards,
Gerard.
is understandably causing a segfault:
//---- begin code ----
#include <valarray>
#include <iostream>
using namespace std;
class SomeClass {
private:
valarray <double> mx;
public:
SomeClass(valarray <double>& x) {mx = x;}
void func() {cout << mx[1];} // <---- Segfault here.
};
int main () {
valarray <double> x(5,10);
SomeClass foo(x);
foo.func();
return 0;
}
//---- end code ----
It will fail at runtime because, as I understand it, mx is a zero-size
valarray and hence mx[1] does not exist.
Yet (for a simple numerical code) I would like something similar to the
above to work, i.e. I'd like to pass a valarray of predefined size to
the constructor of a class to initialize a class-member valarray, say
mx, and after reference to mx's elements as mx[0], mx[1] etc. As a C++
starter I'm confused about the best way to work around the problem in
the code above. Any advice would be welcome.
Regards,
Gerard.