Topic: Template Classes


Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/16
Raw View
"Mark M. Young" wrote:
>
> > To get the effect you want, you need to add the export keyword to your
> > template declaration. That will make the definition in Matrix.cpp
> > exported. Note that compiler support for the export keyword is rather
> > spotty at the moment.
> >
>
> Could you explain that where I don't have to guess whether you know what
> declaration means? ...

I'm not sure why you're wondering about that.
If a template class member is declared 'export', any definition provided
in the same translation unit is an exported definition. Only one
exported definition is allowed per program. Uses of members declared
'export' in other translation units all use the same exported
definition. See section 14 p6-9 for more details.

> ... Are you speaking of putting 'export{ ... }' around the
> "class template"?

Not exactly; the correct syntax for exporting an entire class is shown
below. You can also export individual members of a template. Doing
'export' at the class level is equivalent to exporting every exportable
member of the template. I've never used the export keyword; it's new
with C++98, and few compilers support it; none that I have access to. I
believe that the following is the only change you need to make, in order
to get the effect you want on an implementation that supports the
keyword:

matrix.h:

 #ifndef MatrixH
 #define MatrixH

 export template <class T> class Matrix
 {
 public:
  Matrix(int, int);
 private:
  T **whatever;
  int m, n;
 };

 #endif

Other people have shown you alternative ways to get around the problem
when the compiler doesn't support exported templates.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Mark M. Young" <youngmm@wku.edu>
Date: 1999/11/13
Raw View
> To get the effect you want, you need to add the export keyword to your
> template declaration. That will make the definition in Matrix.cpp
> exported. Note that compiler support for the export keyword is rather
> spotty at the moment.
>

Could you explain that where I don't have to guess whether you know what
declaration means?  Are you speaking of putting 'export{ ... }' around the
"class template"?

Mark M. Young
YoungMM@Hera.WKU.edu
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Terje Sandstr m" <tsandstr@online.no>
Date: 1999/11/11
Raw View
As James Kuyper Jr. pointed out, the support for the 'export' keyword is
sparse.
So what you got to do is either:

Include matrix.cpp (which you, by the way, should rename to something like
MatrixImplementation.h) in the files where you need to use the Matrix.

Or

explicit instantiate the classes you need.

Assume you have renamed as above:
Make a file Matrix.cpp

#include "Matrix.h"
#include "MatrixImplementation.h"
template class Matrix<double>;
template class Matrix<int>;

Now you have explicitly instantiated Matrix for double's and int's.

In your Other file, you now only need to include  matrix.h, with the
declarations.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Your Name" <Your.Address@lboro.ac.uk>
Date: 1999/11/09
Raw View
I have a problem with template classes in C++.  If I have the following =
in Matrix.h

#ifndef MatrixH
#define MatrixH

template <class T> class Matrix
{
public:
        Matrix(int, int);
private:
        T **whatever;
        int m, n;
};

#endif

, the following in Matrix.cpp

#include "Matrix.h"

template <class T> Matrix<T>::Matrix(int i, int j)
{
        m =3D i;
        n =3D j;
}

, and the following in another .cpp file

#include "Matrix.h"

void main()
{
        Matrix <double> my_matrix(3, 4);
}

, why do I get the message about unresolved external =
'Matrix<double>::Matrix<double>(int, int)'?

Please help!!

Mark

Please email me in person at M.C.Thomas-95@student.lboro.ac.uk
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Mark M. Young" <youngmm@wku.edu>
Date: 1999/11/10
Raw View
My guess is that you're using GCC or something like that.  For some reason
(presumably because of late binding), the header can't "see" the
'Matrix.cpp' contents.  So, '#include'ing 'Matrix.cpp' in either 'Matrix.h'
or the other cpp file.  I know compilers need to get a move on, but that's
the workaround for now.

Mark M. Young

e-mail me at YoungMM@Hera.WKU.edu ... I've got something you'll like

Your Name <Your.Address@lboro.ac.uk> wrote in article
<01bf2ad2$1d6a0790$6a29e783@s006lab32>...
I have a problem with template classes in C++.  If I have the following in
Matrix.h

#ifndef MatrixH
#define MatrixH

template <class T> class Matrix
{
public:
        Matrix(int, int);
private:
        T **whatever;
        int m, n;
};

#endif

, the following in Matrix.cpp

#include "Matrix.h"

template <class T> Matrix<T>::Matrix(int i, int j)
{
        m = i;
        n = j;
}

, and the following in another .cpp file

#include "Matrix.h"

void main()
{
        Matrix <double> my_matrix(3, 4);
}

, why do I get the message about unresolved external
'Matrix<double>::Matrix<double>(int, int)'?

Please help!!

Mark

Please email me in person at M.C.Thomas-95@student.lboro.ac.uk
---


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/10
Raw View
Your Name wrote:
>
> I have a problem with template classes in C++.  If I have the following in Matrix.h
>
> #ifndef MatrixH
> #define MatrixH
>
> template <class T> class Matrix
> {
> public:
>         Matrix(int, int);
> private:
>         T **whatever;
>         int m, n;
> };
>
> #endif
>
> , the following in Matrix.cpp
>
> #include "Matrix.h"
>
> template <class T> Matrix<T>::Matrix(int i, int j)
> {
>         m = i;
>         n = j;
> }
>
> , and the following in another .cpp file
>
> #include "Matrix.h"
>
> void main()
> {
>         Matrix <double> my_matrix(3, 4);
> }
>
> , why do I get the message about unresolved external 'Matrix<double>::Matrix<double>(int, int)'?

Section 14 p8: "A non-exported template that is neither explicitly
specialized nor explicitly instantiated must be defined in every
translation unit in which it is implicitly instantiated (14.7.1) or
explicitly instantiated (14.7.2)".

To get the effect you want, you need to add the export keyword to your
template declaration. That will make the definition in Matrix.cpp
exported. Note that compiler support for the export keyword is rather
spotty at the moment.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]