Coder's Guild Mailing List

Re: C++ (still a problem)

Posted by Computer Consultant on 1999-04-03

I don't know for sure but I think that object's are placed directly in the
code
and a template is made at compile time so you would have to declare each
one seperately.

If not, then there's a compile option you're missing.

I booted linux to see this, I cated the .o to see what it produced and it
screwed up my VT100 emulation (Like those old pine email bombs or those IRC
flash bombs with out the
modem rz/sz rev/send codes to dissconnect you) and I had to logout and log
back in.

Then I started screen and did man on g++ and I tried to open a new window
and it locked
up my comptuer.

There was only one option for templates anyway, you may want to man g++ and
try what
it said, it was about external templates, but I think it was just to keep
file sizes
down.

I haven't done C++ in a while and I always made libs for classes never
linked objects.


At 10:03 PM 4/3/99 +0200, you wrote:
>Computer Consultant wrote:
>> 
>> Of course, try making it a .lib
>> 
>
>I don't want to make a .lib I want to use it like I have it. 
>
>The code will work fine if I put it all in 1 file and compile it but I
>want it separated out into its own files.
>
>And as soon as I drop the templates out of it, it compiles and links
>fine. With the templates it causes undefined references.
>
>**without templates:
>
>[frank@FranksPC test]$ make
>g++ -c test.cc -Wall -O2 -m486 -s
>g++ -o test main.o test.o -Wall -O2 -m486 -s
>[frank@FranksPC test]$ ./test 
>hello,world
>
>**with templates:
>
>[frank@FranksPC test]$ make
>g++ -c main.cc -Wall -O2 -m486 -s
>g++ -c test.cc -Wall -O2 -m486 -s
>g++ -o test main.o test.o -Wall -O2 -m486 -s
>main.o: In function `main':
>main.o(.text+0x10): undefined reference to `test<char *>::print(char *)'
>collect2: ld returned 1 exit status
>make: *** [test] Error 1
>[frank@FranksPC test]$ 
>
>
>Again I stress that the templates work fine only if I put all the code
>in 1 file and compile and link. Does anyone know whats wrong with this
>code? There are 4 files
>
>test.h
>test.cc
>main.cc
>makefile
>
>// Test header
>#ifndef TEST_H
>#define TEST_H
>
>#include <iostream.h>
>
>template <class t>
>class test {
>
>public:
>   test() {}
>
>   void print(t a);
>
>};
>
>#endif
>
>// test.cc
>#include "test.h"
>
>template <class t>
>void test<t>::print(t a) 
>{ 
>   cout << a; 
>}
>
>// main.cc
>#include "test.h"
>
>int main()
>{
>   test<char*> t;
>   t.print("hello,world\n");
>
>return 0;
>}
>
># Makefile
>CC=g++
>OPS=-Wall -O2 -m486 -s
>
>test: main.o test.o
>   $(CC) -o test main.o test.o $(OPS)
>
>main.o: main.cc
>   $(CC) -c main.cc $(OPS)
>
>test.o: test.cc
>   $(CC) -c test.cc $(OPS)
>   
>clean:
>   rm test *.o core
>
>
>-- 
>From:      Frank Hale
>Email:     frankhale@xxxxxxxx.xxx.xxx   
>ICQ:       7205161         
>Website:   http://www.franksstuff.com
>
>
                +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+
                | http://www.nauticom.net/www/secret  |
                +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=+
3%