Simple template syntax question

Simple template syntax question

Post by rsin » Sat, 26 Jul 2003 20:12:25



Hi all, I am a newbie playing with templates and I thought the syntax is
simple enough that I could not mess it up. But apparently I did, and I
can't figure out what is wrong. Could some caring soul please tell me what
I am doing wrong here? Thanks.

I have declared a function template in my header file

     using TNT::Array1D;
namespace RSINA{

     template <class T>
     Array1D<T> SubVectorByIndex(const Array1D<T> &x, int min, int max);
         ...
         ...

and defined it in my source file as

namespace  RSINA{
     template <class T>
     Array1D<T> SubVectorByIndex(const Array1D<T> &x,
                                      int min, int max)
         {
         ...
         ...
         }
         ...

When I try to invoke the function in my main file,

    Array1D<double> t1(100);
    Array1D<double> t1_sub= RSINA::SubVectorByIndex(t1,5,10);

I get the following error with g++ on RedHat 9:

main_program.o(.text+0x2a): In function `main':
: undefined reference to `TNT::Array1D<double>
RSINA::SubVectorByIndex<double>(TNT::Array1D<double> const&, int, int)'
collect2: ld returned 1 exit status
make: *** [program] Error 1

But if I don't declare and don't define the function as template, then the
code compiles without error, i.e. when I say

     using TNT::Array1D;
namespace RSINA{

     Array1D<double> SubVectorByIndex(const Array1D<double> &x, int min, int
max);
         ...
         ...

and defined it in my source file as

namespace  RSINA{
     Array1D<double> SubVectorByIndex(const Array1D<double> &x,
                                      int min, int max)
         {
         ...
         ...
         }
         ...

  then everything is fine.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Mikael Brockma » Sun, 27 Jul 2003 21:47:57


 > Hi all, I am a newbie playing with templates and I thought the syntax is
 > simple enough that I could not mess it up. But apparently I did, and I
 > can't figure out what is wrong. Could some caring soul please tell me what
 > I am doing wrong here? Thanks.

Don't worry, most people are struck by this problem sooner or later.

The compiler is not smart enough to figure out by itself which instances
of the template you use, and for obvious reasons, it can't compile every
possible instance into the object file.  The absolutely simplest solution
is to make sure everyone who uses the template knows its implementation as
well as its interface: just inline all templated classes and functions in
the header file, like this:

  foo.hh:
   template <typename T>
   class Foo {
   private:
     T bar_;

   public:
     Foo (T bar) : bar_ (bar)
     { }

     T getBar ()
     { return bar_; }
   };

  test.cc:
   #include "foo.hh"
   #include <iostream>

   int main ()
   {
     Foo<double> myFoo (3.14159);
     std::cout << myFoo.getBar () << std::endl; // prints 3.14159
   }

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Dhru » Sun, 27 Jul 2003 21:52:50


[snip]......

For templates, you will have to put all the definitons in the header
file itsef, because the compiler instantiates the functions (or
whetever) at compile time, and it needs to see the definition. Or, you
could explicitly instantiate the call to the function at the end of the
header file for a double, and continue what you are doing.

Regards,
-Dhruv.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by llewell » Sun, 27 Jul 2003 22:06:34


 > Hi all, I am a newbie playing with templates and I thought the syntax is
 > simple enough that I could not mess it up. But apparently I did, and I
 > can't figure out what is wrong. Could some caring soul please tell me what
 > I am doing wrong here? Thanks.
 >
 >
 > I have declared a function template in my header file
 >
 >      using TNT::Array1D;
 > namespace RSINA{
 >
 >      template <class T>
 >      Array1D<T> SubVectorByIndex(const Array1D<T> &x, int min, int max);
 >          ...
 >          ...
 >
 >
 > and defined it in my source file as
[snip]

Put the definition of the template in the header file, or in
     another file #included by the file that contains the instantiation
     of the template. g++ does not support seperate compliations of
     templates.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Shantanu Gar » Sun, 27 Jul 2003 22:45:02



Quote:> Hi all, I am a newbie playing with templates and I thought the syntax is
> simple enough that I could not mess it up. But apparently I did, and I
> can't figure out what is wrong. Could some caring soul please tell me what
> I am doing wrong here? Thanks.

> I have declared a function template in my header file

>      using TNT::Array1D;
> namespace RSINA{

>      template <class T>
>      Array1D<T> SubVectorByIndex(const Array1D<T> &x, int min, int max);
>          ...
>          ...

> and defined it in my source file as

> namespace  RSINA{
>      template <class T>
>      Array1D<T> SubVectorByIndex(const Array1D<T> &x,
>                                       int min, int max)
>          {
>          ...
>          ...
>          }
>          ...

> When I try to invoke the function in my main file,

>     Array1D<double> t1(100);
>     Array1D<double> t1_sub= RSINA::SubVectorByIndex(t1,5,10);

> I get the following error with g++ on RedHat 9:

> main_program.o(.text+0x2a): In function `main':
> : undefined reference to `TNT::Array1D<double>
> RSINA::SubVectorByIndex<double>(TNT::Array1D<double> const&, int, int)'
> collect2: ld returned 1 exit status
> make: *** [program] Error 1

Try defining function inside header file. It will solve your problem.

-Shantanu Garg

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Francis Glassboro » Mon, 28 Jul 2003 01:07:21




Quote:>I have declared a function template in my header file

>     using TNT::Array1D;
>namespace RSINA{

>     template <class T>
>     Array1D<T> SubVectorByIndex(const Array1D<T> &x, int min, int
max);
>         ...
>         ...

>and defined it in my source file as

and that, I suspect, is the cause of your problems. Most compilers
require what is called the inclusion model which means that definitions
of templates must be visible at the point of use (in effect you have to
place them in a file that is included in the file using them)

--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow      ACCU

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Alf P. Steinba » Mon, 28 Jul 2003 01:08:33




Quote:>Hi all, I am a newbie playing with templates and I thought the syntax
>is simple enough that I could not mess it up. But apparently I did, and
>I can't figure out what is wrong. Could some caring soul please tell me
>what I am doing wrong here? Thanks.

At the point where a template is used the compiler needs access to the
template definition, the text, which means: put the template definition
in your header file.

There are ways of compiling template instantiations for specific types
separately.  There is also an "export" keyword that few if any compilers
have managed to implement correctly (I heard that the technical problems
had been solved, but nobody uses "export" yet).  But first try the
above.

Hth.,

"One True"

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by William Magil » Mon, 28 Jul 2003 19:35:27


 > Hi all, I am a newbie playing with templates and I thought the syntax is
 > simple enough that I could not mess it up. But apparently I did, and I
 > can't figure out what is wrong. Could some caring soul please tell me what
 > I am doing wrong here? Thanks.
 >
 >
 > I have declared a function template in my header file
 >

     - snip -

 >
 >
 > and defined it in my source file as
 >
     - snip -

Templates do not play well with the separation model.  ie, you cannot
have the declaration in a header file and the definition in a cpp file -
   both declaration and definition must be in the header.
I feel obligated to make mention here of the export keyword (see
Stroustrup's The C++ Programming Language or any other modern c++ book
with discussion on templates) but it's not widely supported and is
possibly going to get pulled from the Standard.  So just bung it all in
the header file.

- Willow

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Pete Becke » Mon, 28 Jul 2003 19:36:09




> [snip]......

> For templates, you will have to put all the definitons in the header
> file itsef, because the compiler instantiates the functions (or
> whetever) at compile time, and it needs to see the definition.

That's not actually correct. Most compilers require it because they
haven't yet implemented the 'export' keyword. But that's exactly what
export gets at: putting template code in a separate translation unit.

--

"To delight in war is a merit in the soldier,
a dangerous quality in the captain, and a
positive crime in the statesman."
        George Santayana

"Bring them on."
        George W. Bush

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Ulrich Eckhard » Mon, 28 Jul 2003 20:30:56



> I have declared a function template in my header file

[snip]

> and defined it in my source file as

[snip]

> I get the following error with g++ on RedHat 9:

> main_program.o(.text+0x2a): In function `main':
> : undefined reference to `TNT::Array1D<double>
> RSINA::SubVectorByIndex<double>(TNT::Array1D<double> const&, int,
> int)'
> collect2: ld returned 1 exit status
> make: *** [program] Error 1

It's explained in the FAQ (see my .sig).

Uli

--
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Daniel Seit » Mon, 28 Jul 2003 20:32:02


You have to use the inclusion model. Basically, this means that you must
put all the definitions in headers, and each translation unit includes
that file.

ie,:

// max.h

#ifndef  MAX_H
#define MAX_H

template<typename T>
inline T const& max(T const& a, T const& b) {
  return a > b? a : b;

Quote:}

#endif

// main.cc

#include "max.h"

int
main() {
  int x = 10, y = 12;
  max(x,y); // calls max<int>

Quote:}

Note, you don't have to use 'inline'.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Simple template syntax question

Post by Dhru » Tue, 29 Jul 2003 18:55:01


 >>

 >>
 >> [snip]......
 >>
 >> For templates, you will have to put all the definitons in the header
 >> file itsef, because the compiler instantiates the functions (or
 >> whetever) at compile time, and it needs to see the definition.
 >>
 >
 > That's not actually correct. Most compilers require it because they
 > haven't yet implemented the 'export' keyword. But that's exactly what
 > export gets at: putting template code in a separate translation unit.
 >
 > --

Yes, you're right, but the OP is using g++3.2 on RH9, which definitely
does not have 'export' support. FWIW, I guess, only Comeau has that
support as of now.

Regards,
-Dhruv.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. Simple question (Specialized templates within non-specialized templates)

Hi all you C++ gurus,

Consider this small example:

template<typename T, int I> class A {} ;

class B {};   // no templates
class C {};

 I have an explicitly specialized function that returns different
stuff based on what is the template parameter:

template <typename T> func();
template<> int func<B>();
template<> int func<C>();

Now I want this function to serve the A. BUT

template<typename T, int I>
template<> function<A<T,I> > func() {};

is illegal, because the standard prohibits specialization of a
template within a NON-specialized template.

Any workarounds?

Thanks!!


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. Exporting to PVCS

3. Simple Scripting Syntax Question (I hope) ConnectServer Optional Parameters

4. Cisco ISDN to Windows RAS ISDN

5. simple syntax question on destructors and copy constructors

6. IWANNT97 Call For Papers

7. Templates question: Whats the proper syntax here?

8. The letter "L"

9. Template syntax question

10. Template Specialization syntax question

11. Syntax for declaring friend function template in class template

12. Syntax for member template of template class