By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,676 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,676 IT Pros & Developers. It's quick & easy.

Problem with basic templates on GCC

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Winbatch wrote:
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()
{
std::cout<<temp;
}

Test.cpp
------------------------------------------

// What's this? #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


It compiles fine here with g++ 3.4.2. Which version are you using? My
exact code:
#include <iostream>
#include <string>
template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};
template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
std::cout<<temp;
}
int main()
{
using namespace std;
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2

P: n/a
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" <wi******@techie.com> wrote in message
news:nd*********************@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.

Jul 23 '05 #3

P: n/a

"HappyHippy" <ka******@mail.ru> wrote in message
news:d1**********@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" <wi******@techie.com> wrote in message
news:nd*********************@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?
Jul 23 '05 #4

P: n/a

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1110938952.429507@athnrd02...
Winbatch wrote:
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()
{


std::cout<<temp;
}

Test.cpp
------------------------------------------

// What's this? #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


It compiles fine here with g++ 3.4.2. Which version are you using? My
exact code:
#include <iostream>
#include <string>
template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};
template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
std::cout<<temp;
}
int main()
{
using namespace std;
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

--
Ioannis Vranos

http://www23.brinkster.com/noicys


Ioannis,
Can you provide the compile command you used?
Jul 23 '05 #5

P: n/a
> It compiles fine here with g++ 3.4.2. Which version are you using? My
exact code:

2.95
Jul 23 '05 #6

P: n/a
HH,

Your sample indicates that I should include the .hpp from the .h. Is that
right? Wouldn't the class need to be defined before the implemenation? (ie
shouldn't the .hpp include the .h instead?)

Winbatch
Jul 23 '05 #7

P: n/a
Winbatch wrote:
It compiles fine here with g++ 3.4.2. Which version are you using? My
exact code:


2.95

There's your problem. Upgrade to a more recent version if you can (2.95
had some problems with template code).

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #8

P: n/a
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:39*************@individual.net...
Winbatch wrote:
It compiles fine here with g++ 3.4.2. Which version are you using? My
exact code:


2.95

There's your problem. Upgrade to a more recent version if you can (2.95
had some problems with template code).

HTH,
--ag


I thought I was seriously going nuts here. Unfortunately, it's not my
machine so I don't have much control. What do you think of the .hpp route
mentioned previously? I'm trying to implement it on that machine..
Jul 23 '05 #9

P: n/a
HH,

Just wanted to thank you - your suggestion to use the .hpp's not only helped
me to successfully compile on GCC 2.95 on NetBSD, but also on AIX 5.1 using
/usr/vacpp/bin/xlC. (Something I was struggling to accomplish as well).

Thanks again,
Winbatch
Jul 23 '05 #10

P: n/a
Winbatch wrote:
HH,

Just wanted to thank you - your suggestion to use the .hpp's not only helped
me to successfully compile on GCC 2.95 on NetBSD, but also on AIX 5.1 using
/usr/vacpp/bin/xlC. (Something I was struggling to accomplish as well).

Thanks again,
Winbatch

I missed the call before.

However, one point to be made is that as (virtually -- there's Comeau
and something else I believe) no one supports `export', there seems to
be limited utility in separating the templated class definition from its
templated member functions.[1] Where one needs to be visible, at least
some subset of the rest would need to be visible too.

Think of templates as a away of telling the compiler to generate code
for you on demand -- not as code itself. The definitions need to be
visible in any translation unit that use them.

HTH,
--ag

[1] Well, when we're dealing with small projects anyway; even in large
projects any underlying file structure should be transparent to its
clients. `#include'-ing a single `.h' file should be sufficient.
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #11

P: n/a
Winbatch wrote:
Ioannis,
Can you provide the compile command you used?

#include <iostream>
#include <string>
template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};
template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
std::cout<<temp;
}
int main()
{
using namespace std;
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

C:\c>g++ temp.cpp -o temp.exe

C:\c>temp
In Test
TEST
C:\c>g++ -v
Reading specs from C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as
--host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls
--enable
-languages=c,c++,f77,ada,objc,java --disable-win32-registry
--disable-shared --e
nable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x
--enable-ja
va-gc=boehm --disable-libgcj-debug --enable-interpreter
--enable-hash-synchroniz
ation --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.2 (mingw-special)

C:\c>

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #12

P: n/a
Yes you are completely right.
Sorry.
It should look like this:

MyTemp.h
--------------------------
#include <iostream>

template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};

#include "MyTemp.hpp"
-------------------------

Regards,
HappyHippy

"Winbatch" <wi******@techie.com> wrote in message
news:ye*********************@twister.nyc.rr.com...
HH,

Your sample indicates that I should include the .hpp from the .h. Is that
right? Wouldn't the class need to be defined before the implemenation?
(ie shouldn't the .hpp include the .h instead?)

Winbatch

Jul 23 '05 #13

P: n/a
There is a way to make it work without #include 'ing implementation to
declaration. Keyword "export" should be used for this purpose according to
the standard. BUT. Neither VC++ 7.1 nor gcc 3.3.x implement this feature as
far as I know.

It works in Ioannis' example because (as I understood from his post) he put
everything in a single file. So it is almost the same as using #include
which I suggested to use.

Regards,
HappyHippy

"Winbatch" <wi******@techie.com> wrote in message
news:N1*********************@twister.nyc.rr.com...

"HappyHippy" <ka******@mail.ru> wrote in message
news:d1**********@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" <wi******@techie.com> wrote in message
news:nd*********************@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?

Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.