public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Help compiling cpp program with templates
@ 2004-01-07 11:37  
  2004-01-07 16:37 ` Claudio Bley
  2004-01-07 17:00 ` Eljay Love-Jensen
  0 siblings, 2 replies; 3+ messages in thread
From:   @ 2004-01-07 11:37 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 613 bytes --]

  I tried to use a simple template in a class, and it
worked when all the definition where in the same file.
But when I separated the code in two cpp files, I get
a linker error, and I couldn't firgure out how to
compile it.
 I tried the following commands:
  g++ -c test.cpp
  g++ -c test1.cpp
  g++ -o test test.o test1.o

The first two worked, but i got the linker error at
the last one.
I attached the files to these mail.
Thank you.

__________________________________________________________________

Gesendet von Yahoo! Mail - http://mail.yahoo.de
Logos und Klingeltöne fürs Handy bei http://sms.yahoo.de

[-- Attachment #2: test.cpp --]
[-- Type: application/octet-stream, Size: 953 bytes --]


#include <stdio.h>
#include <stdlib.h>
#include "test.h"


class cont
   {
     public:
        test<double> t1;
        test<char> *t2;


        cont()
           {
              t2 = new test<char>();
              t1.d2[0] = 333;
               printf("\ncont t1 %X %X %X",&t1,t1.d1,t1.d2);
               printf("\ncont t2 %X %X %X",t2,t2->d1,t2->d2);
           };
        ~cont(){};

        test<double>& Get1(){ return t1;};
        test<char>& Get2(){ return *t2;};
   };

void fun(test<int>& t)
{
  printf("\nfun t %X %X %X",&t,t.d1,t.d2);
}

void fun1(test<int> t)
{
  printf("\nfun t %X %X %X",&t,t.d1,t.d2);
}


int main(void)
{
  test<int> t1;
  test<int> *t2 = new test<int>();
  cont c;
  printf("\nt1 %X %X %X %d",&t1,t1.d1,t1.d2,t1.d2[0]);
  printf("\nt2 %X %X %X",t2,t2->d1,t2->d2);
  fun(t1);
  fun(*t2);
  fun1(t1);
  fun1(*t2);
  //<int>t1 = c.Get1();
  printf("\nt1 %X %X %X %d",&t1,t1.d1,t1.d2,t1.d2[0]);
  printf("\n\n");

}


[-- Attachment #3: test.h --]
[-- Type: application/octet-stream, Size: 143 bytes --]



template <class T> class test
   {
     public:
        int *d1;
        int d2[20];
        T t;
        test();
        ~test(){};
   };



[-- Attachment #4: test1.cpp --]
[-- Type: application/octet-stream, Size: 117 bytes --]


#include "test.h"

template <class T> test<T>::test()
   {
      d1 = new int[10];
      //d2 = new int[20];
   };


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Help compiling cpp program with templates
  2004-01-07 11:37 Help compiling cpp program with templates  
@ 2004-01-07 16:37 ` Claudio Bley
  2004-01-07 17:00 ` Eljay Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: Claudio Bley @ 2004-01-07 16:37 UTC (permalink / raw)
  To: gcc-help

On Wed, Jan 07, 2004 at 12:37:53PM +0100,   wrote:
>   I tried to use a simple template in a class, and it
> worked when all the definition where in the same file.
> But when I separated the code in two cpp files, I get
> a linker error, and I couldn't firgure out how to
> compile it.
>  I tried the following commands:
>   g++ -c test.cpp
>   g++ -c test1.cpp
>   g++ -o test test.o test1.o
> 
> The first two worked, but i got the linker error at
> the last one.
> I attached the files to these mail.
> Thank you.

You can't seperate a template class and its constructor like this.
A template class with all its methods must be fully available at 
instantiation time (as of now -- this may change in the future when the
`export' keyword is supported).

Usually you do it like this:

// test.h

#ifndef TEST_H
#define TEST_H

template <class T> foo {
  ...
  test();
};

#include <test.tcc>

#endif // TEST_H

----------------------------------------------------------------------

// test.tcc
// (gets included from test.h)

template <class T> test<T>::test( ) {
}

----------------------------------------------------------------------

// test.cc

#include "test.h"

...

----------------------------------------------------------------------

Of course, you may choose another suffix instead of .tcc for your
template implementation files, but that's the same suffix libstdc++ uses 
for these files.

HTH
-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux user                         - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Help compiling cpp program with templates
  2004-01-07 11:37 Help compiling cpp program with templates  
  2004-01-07 16:37 ` Claudio Bley
@ 2004-01-07 17:00 ` Eljay Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2004-01-07 17:00 UTC (permalink / raw)
  To: nutrina999, gcc-help

Hi nutrina999,

The TEMPLATE code in test11.cpp should be in the header (test.h) file, not in a source (.cpp) file.

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-01-07 17:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-07 11:37 Help compiling cpp program with templates  
2004-01-07 16:37 ` Claudio Bley
2004-01-07 17:00 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).