I'm trying to use the blitz++ matrix library in my code (http://www.oonumerics.org/blitz/) but am getting frustrated with how to use the blitz templates in my classes. All examples I have seen for this and any template simply use them in the main() function, which I cannot do for my program.
For example, to create a 3x3 array using blitz, their example is :
#include <blitz/array.h>
usingnamespace blitz;
int main()
{
Array<float,2> A(3,3), B(3,3), C(3,3);
A = 1, 0, 0,
2, 2, 2,
1, 0, 0;
B = 0, 0, 7,
0, 8, 0,
9, 9, 9;
C = A + B;
cout << "A = " << A << endl
<< "B = " << B << endl
<< "C = " << C << endl;
return 0;
}
Now this is fine so long as I'm restricting myself to the main routine, but I'm not and cannot do this. I want to achieve the following:
blitz.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <cstdio>
#include <iostream>
#include <blitz/array.h>
class Test {
public:
Test();
~Test();
void showd();
private:
blitz::Array<double,2> d(3,3); // ** THIS IS NOT ALLOWED BUT I DON'T KNOW HOW TO CREATE A (3,3) ARRAY IN A CLASS
};
#include "blitz.hpp"
usingnamespace std;
usingnamespace blitz;
int main( int argc, constchar* argv[] ){
Array<double,2> c(3,3);
c = 0;
c(0,0) = 1;
c(1,1) = 1;
c(2,2) = 1;
//cout << c << endl;
Test *t = new Test();
t->showd();
delete t;
exit(0);
}
Test::Test()
{
//c = new blitz::Array<double,2>(3,3);
//*c = 0;
blitz::Array<double,2> c(3,3);
blitz::Array<double,2> d(3,3); // this creates a LOCAL d, doesn't create a class instance of d to be 3,3
c = 0;
cout << c << endl;
}
Test::~Test()
{
}
void Test::showd()
{
d = 1;
cout << d << endl;
}
There MUST be a way of doing this otherwise there is no point in having Templates in C++ !
I've never used blitz before and your question puzzles me somewhat. But if you continue having trouble try storing a pointer to the blitz array in your Test class and instantiate it with the 'new' keyword in Test's constructor.
no, I meant it creates d local to the constructor. Once the constructor returns, that particular d no longer exists.
quirkyusername,
I've thought of that, but if I use a pointer I don't know then:
a) how to initialise the size of the matrix (blitz calls them arrays) to the size I want i.e. c = new blitz::Array<double,2> c(3,3) isn't going to work.
b) I've no idea how to access the various functions of the array if I'm using a pointer to it.