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

This is a discussion on How do I use a template in a class NOT in the main() method? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use the blitz++ matrix library in my code ( Blitz++ Home Page ) but am getting frustrated ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    24

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

    I'm trying to use the blitz++ matrix library in my code (Blitz++ Home Page) 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 :

    Code:
    #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
    Code:
    #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

    Code:
    #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

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    35
    To access a non-default constructor of a class member:

    Code:
    class test {
    private:
        std::string name;
    public
        test();
        ~test();
    }
    
    // The following line matters:
    test::test() : name("A name") {
    }
    Also known as the constructor initialization list.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    24
    Quote Originally Posted by Florian View Post
    To access a non-default constructor of a class member:

    Code:
    class test {
    private:
        std::string name;
    public
        test();
        ~test();
    }
    
    // The following line matters:
    test::test() : name("A name") {
    }
    Also known as the constructor initialization list.
    Thanks Florian, this is what I needed. Thanks for your help ! :

    blitz.hpp:
    Code:
    #include <cstdio>
    #include <iostream>
    #include <blitz/array.h>
    
    class Test {
    	public:
    	Test();
    	~Test();
    	void showc();
    	
    	private:
    	blitz::Array<double,2> d;
    };
    blitz.cpp
    Code:
    #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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to define a static member in a template class
    By wanziforever in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2009, 04:44 AM
  2. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 12:46 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21