CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    3

    template costructor doesn't work so well

    Hello everybody!

    I realized a Matrix class to practice and I have a problem I can not solve!

    Here my problematic code:

    Mtrx.h:
    Code:
    template <class T>
    Mtrx::Mtrx(dim m, dim n, const bool random_constructed = false, const T min = static_cast<T>(0), const T max = static_cast<T> (10))
    Mtrx.C
    Code:
    #include "Mtrx4.hxx"
    #include <stdexcept>      
    #include <utility>       
    #include <iomanip>     
    #include <random>    
    #include <ctime>     
    
    
    std::allocator<double> Mtrx::alloc;
    
    
    inline Mtrx::Mtrx():
      NRows(0), NColumns(0), mtrx_begin(Mtrx::alloc.allocate(0)), mtrx_end(mtrx_begin), constructed(false) {}
    
    
    
    template <class T>
    Mtrx::Mtrx(dim m, dim n, const bool random_constructed, const T min, const T max):
    
      NRows(m), NColumns(n), mtrx_begin(Mtrx::alloc.allocate(m*n)), mtrx_end(mtrx_begin+(m*n)), constructed(random_constructed)
    
    {
    
    	if(random_constructed){
    
    
        		control(typeid(double) == typeid(min) && typeid(double) == typeid(max) ||
    			typeid(int) == typeid(min) && typeid(int) == typeid(max),
    
    	    		"ERRORE! Il costruttore pu� generare matrici random solo di tipo int o double");
    
        
        	if( typeid(int) == typeid(min) ){
    
          			std::default_random_engine e(std::time(0));
    
            		std::uniform_int_distribution<> u( min, max);
    
          
      	
    			for(ppp i=mtrx_begin; i<mtrx_end; ++i)	*i=u(e);
    
        		} else {
    
          			std::default_random_engine e(std::time(0));
    
          			std::uniform_real_distribution<> u( min, max);
    
          
      	
    			for(ppp i=mtrx_begin; i<mtrx_end; ++i)  *i=u(e);
    
        		}
    
      	}
    
    }
    And here the relative main section:
    Code:
    Mtrx rand1 ( 5, 5, bool);		// ok
    cout<<rand1<<endl;
    
    Mtrx rand2 ( 7, 3, bool, -5, 20);	// ok
    cout<<rand2<<endl;
    
    Mtrx rand3 ( 7, 7, bool, 0., 15.);	// compilation error: undefined reference to 
    					// "Mtrx::Mtrx<double>(unsigned long, unsigned, bool, double, double)"
    					// collect2: error: ld returned 1 exit status

    I compiled in a Linux OS with g++ -std=c++11

    Sorry for my bad english.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: template costructor doesn't work so well

    Quote Originally Posted by therover View Post
    Hello everybody!

    I realized a Matrix class to practice and I have a problem I can not solve!
    You need to give us more information.

    First, are you compiling the template code separately in another module? If you are, then maybe that is your problem. Template class definition and declaration must be in the same compilation unit, not separate files. I would expect to see something like this:
    Code:
    #ifndef MTRX_H
    #define MTRX_H
    
    template <typename T>
    class Mtrx
    {
       ///.... 
    };
    #include "Mtrx.c"
    #endif
    Note that the implementation must be included with the template code. The reason is that the template needs to be instantiated when it is used, and more than likely the instantiation for double doesn't exist at link time.

    If you want a cleaner example, look at the standard headers such as <vector>, <list>, <map>, etc. You see that all the code is within the header, and that is what you should be doing in your example. So remove Mtrx.C from the list of files that are being compiled, and instead include it in the header.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2013
    Posts
    3

    Re: template costructor doesn't work so well

    Thanks very much! It was the problem!
    And sorry for the stupid question!

    I realize few time later, but 'cause of an upcoming exame I did not reply immediatly!

    Is it a correct style if I have, however, left the declaration part in the Mtrx.C?

    I just added these two lines after the template costructor definition:

    Code:
    template Mtrx::Mtrx(dim m, dim n, const bool random_constructed, const int min, const int max);
    template Mtrx::Mtrx(dim m, dim n, const bool random_constructed, const double min, const double max);

  4. #4
    Join Date
    Feb 2013
    Posts
    3

    Re: template costructor doesn't work so well

    Of course, doing so, I compiled Mtrx.C and othe files that I have to compile and I included only Mtrx.h!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured