Re: Problem with basic templates on GCC
From: Winbatch (winbatch_at_techie.com)
Date: 03/16/05
- Next message: Winbatch: "Re: Problem with basic templates on GCC"
- Previous message: HappyHippy: "Re: Problem with basic templates on GCC"
- In reply to: HappyHippy: "Re: Problem with basic templates on GCC"
- Next in thread: HappyHippy: "Re: Problem with basic templates on GCC"
- Reply: HappyHippy: "Re: Problem with basic templates on GCC"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 16 Mar 2005 02:41:49 GMT
"HappyHippy" <karenman@mail.ru> wrote in message
news:d186j7$3mb$1@charm.magnus.acs.ohio-state.edu...
> The problem is that at the point of instantiation (MyTemp<string>
> mt("TEST");, Test.cpp) compiler sees only declaration of template class
> member functions (from MyTemp.h file) and no their definitions.
> You should #include MyTemp.cpp into Test.cpp file. Because for template
> classes definions (not only declarations) of all members must be visible
> at the point of instantiation.
> So you should change
> #include <MyTemp.h>
> to
> #include "MyTemp.cpp"
> in Test.cpp file.
>
> But #include 'ing cpp file looks ugly.
> I would advise you to rewrite your code as follows:
>
> MyTemp.h
> --------------------------
> #include <iostream>
> #include "MyTemp.hpp"
>
> template <class T> class MyTemp
> {
> private:
> T temp;
>
> public:
> MyTemp (T temp1);
> void print();
>
>
> };
>
> MyTemp.hpp hpp is commonly used extension for templates
> implementation files
> ----------------------------------------
> template<class T> MyTemp<T>::MyTemp( T temp1 )
> {
> temp = temp1;
> }
> template<class T> void MyTemp<T>::print()
> {
> cout<<temp;
> }
>
> Test.cpp
> ------------------------------------------
> #include <unistd.h>
> #include <iostream>
> #include <string>
> #include "MyTemp.h"
> using namespace std;
> int main()
> {
> cout<<"In Test"<<endl;
> MyTemp<string> mt("TEST");
> mt.print();
> return 0;
> }
>
> There is a VERY GOOD book on C++ templates "C++ Templates: The Complete
> Guide" by David Vandevoorde, Nicolai M. Josuttis, ISBN : 0-201-73484-2
>
> Good Luck,
> HappyHippy
>
>
> "Winbatch" <winbatch@techie.com> wrote in message
> news:ndMZd.28917$qn2.6352856@twister.nyc.rr.com...
>> Hi,
>> If this should be directed to another group, please let me know...
>> I've been working with templates for a few weeks and have been able to
>> develop some nice code on solaris using the Forte C++ compiler (version
>> 7). However, nothing related to templates seems to be compiling correctly
>> when I use g++ on netbsd. I can't tell if it is either: a problem with
>> my code, a problem with NetBSD, or a problem with GCC. I have tried to
>> create the most basic of tests to illustrate the problem. Could someone
>> tell me why this compiles and runs fine using the Forte compiler but does
>> not with g++?
>>
>> MyTemp.h
>> --------------------------
>> #include <iostream>
>> template <class T> class MyTemp
>> {
>> private:
>> T temp;
>>
>> public:
>> MyTemp (T temp1);
>> void print();
>>
>>
>> };
>> MyTemp.cpp
>> ----------------------------------------
>> #include "MyTemp.h"
>>
>> template<class T> MyTemp<T>::MyTemp( T temp1 )
>> {
>> temp = temp1;
>> }
>> template<class T> void MyTemp<T>::print()
>> {
>> cout<<temp;
>> }
>>
>> Test.cpp
>> ------------------------------------------
>> #include <unistd.h>
>> #include <iostream>
>> #include <string>
>> #include <MyTemp.h>
>> using namespace std;
>> int main()
>> {
>> cout<<"In Test"<<endl;
>> MyTemp<string> mt("TEST");
>> mt.print();
>> return 0;
>> }
>>
>> ------------------------------------------------------
>> g++ -c -I./ -g -D_DEBUG -Wall -Wno-parentheses -c *.cpp
>> g++ *.o -o Test
>> /usr/lib/libstdc++.so: warning: reference to compatibility vfork();
>> include <unistd.h> for correct reference
>> Test.o: In function `main':
>> /arpa/ag/d//Test/Test.cpp:7: undefined reference to
>> `MyTemp<basic_string<char, string_char_traits<char>,
>> __default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
>> string_char_traits<char>, __default_alloc_template<false, 0> >)'
>> /arpa/ag/d//Test/Test.cpp:7: undefined reference to
>> `MyTemp<basic_string<char, string_char_traits<char>,
>> __default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
>> string_char_traits<char>, __default_alloc_template<false, 0> >)'
>> /arpa/ag/d//Test/Test.cpp:8: undefined reference to
>> `MyTemp<basic_string<char, string_char_traits<char>,
>> __default_alloc_template<false, 0> > >::print(void)'
>> /arpa/ag/d//Test/Test.cpp:8: undefined reference to
>> `MyTemp<basic_string<char, string_char_traits<char>,
>> __default_alloc_template<false, 0> > >::print(void)'
>> *** Error code 1
>>
>> Stop.
>>
>>
>
>
HH,
Thanks, I will try it. Why would it work in some versions of gcc (as
reported by Ioannis), but not by others? Did it later become part of the
standard?
- Next message: Winbatch: "Re: Problem with basic templates on GCC"
- Previous message: HappyHippy: "Re: Problem with basic templates on GCC"
- In reply to: HappyHippy: "Re: Problem with basic templates on GCC"
- Next in thread: HappyHippy: "Re: Problem with basic templates on GCC"
- Reply: HappyHippy: "Re: Problem with basic templates on GCC"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|