How do I use a template in a class NOT the main() method?

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 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <blitz/array.h>

using namespace 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
};


blitz.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "blitz.hpp"

using namespace std;
using namespace blitz;
int main( int argc, const char* 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++ !

Your help is appreciated
this creates a LOCAL d, doesn't create a class instance of d to be 3,3
What? It's the same thing. A local object is an instance of a class.
Last edited on
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.
helios,

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.

no, I meant it creates d local to the constructor. Once the constructor returns, that particular d no longer exists.
Obviously. The same applies to any local data, templated or not.
Maybe you meant to do something like this?
1
2
3
4
struct A{
    blitz::Array<double,2> a;
    A():a(3,3){}
};
I probably not making myself clear, but I have found the answer. this is what I wanted to achieve :
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 showc();
	
	private:
	blitz::Array<double,2> d;
};


blitz.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "blitz.hpp"

using namespace std;
using namespace blitz;
int main( int argc, const char* argv[] ){
	//cout << c << endl;
	Test *t = new Test();
	t->showc();
	delete t;	
	exit(0);
}

Test::Test() : d(3,3)
{
	blitz::Array<double,2> c(3,3);
	c = 0;
	cout << c << endl;
}

Test::~Test()
{
}

void Test::showc()
{
	d = 2;
	cout << d << endl;
}
Topic archived. No new replies allowed.