Here is an example of a problem, which I tried to reduce to its
bare essentials:
// begin test1.cpp
class E {
public:
template<class T> static void f();
};
template<class T> void E::f() {}
class C {};
int main()
{
E::f<C>();
return 0;
}
// end test1.cpp
This compiles and links fine. Now I split the same code into three
different files:
// begin e.h
class E {
public:
template<class T> static void f();
};
// end e.h
// begin e.cpp
#include "e.h"
template<class T> void E::f() {}
// end e.cpp
// begin test2.cpp
#include "e.h"
class C {};
int main()
{
E::f<C>();
return 0;
}
// end test2.cpp
This compiles fine, but when I try to link (in Visual Studio 2003),
I get this error:
test error LNK2019: unresolved external symbol "public: static void
__cdecl E::f<class C>(void)" (??$f@VC@@@E@@SAXXZ) referenced in
function _main
What is wrong here?
--
Dmitry Epstein
Northwestern University, Evanston, IL. USA
mitia(at)northwestern(dot)edu 6 2143
* Dmitry Epstein <mi**********@northwestern.edu.invalid> schriebt: // begin e.h class E { public: template<class T> static void f(); }; // end e.h
What is wrong here?
The compiler cannot instantiate the template without having access to
the full template definition, which you should simply put in the header
file.
Another possibility is to avoid instantiation at the point of usage by
instantiating the template somewhere else for the relevant types.
But that restricts usage of the template to those types.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* Dmitry Epstein <mi**********@northwestern.edu.invalid> schriebt: // begin e.h class E { public: template<class T> static void f(); }; // end e.h
What is wrong here?
The compiler cannot instantiate the template without having access to
the full template definition, which you should simply put in the header
file.
Another possibility is to avoid instantiation at the point of usage by
instantiating the template somewhere else for the relevant types.
But that restricts usage of the template to those types.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? al***@start.no (Alf P. Steinbach) wrote in
news:40****************@news.individual.net: * Dmitry Epstein <mi**********@northwestern.edu.invalid> schriebt: // begin e.h class E { public: template<class T> static void f(); }; // end e.h
What is wrong here? The compiler cannot instantiate the template without having access to the full template definition, which you should simply put in the header file.
Hm, I know that I can compile a template function all by itself,
and some sort of code is generated at that point. How does the
compiler deal with the absence of a concrete instantiation, and why
does it become a problem elsewhere, like in my example?
Another possibility is to avoid instantiation at the point of usage by instantiating the template somewhere else for the relevant types.
But that restricts usage of the template to those types.
This doesn't sound like what I want to do, but how would I
instantiate the template "somewhere else"? Can you give an
example?
Thanks.
--
Dmitry Epstein
Northwestern University, Evanston, IL. USA
mitia(at)northwestern(dot)edu al***@start.no (Alf P. Steinbach) wrote in
news:40****************@news.individual.net: * Dmitry Epstein <mi**********@northwestern.edu.invalid> schriebt: // begin e.h class E { public: template<class T> static void f(); }; // end e.h
What is wrong here? The compiler cannot instantiate the template without having access to the full template definition, which you should simply put in the header file.
Hm, I know that I can compile a template function all by itself,
and some sort of code is generated at that point. How does the
compiler deal with the absence of a concrete instantiation, and why
does it become a problem elsewhere, like in my example?
Another possibility is to avoid instantiation at the point of usage by instantiating the template somewhere else for the relevant types.
But that restricts usage of the template to those types.
This doesn't sound like what I want to do, but how would I
instantiate the template "somewhere else"? Can you give an
example?
Thanks.
--
Dmitry Epstein
Northwestern University, Evanston, IL. USA
mitia(at)northwestern(dot)edu
On 04 Apr 2004 00:48:47 GMT, Dmitry Epstein
<mi**********@northwestern.edu.invalid> wrote: al***@start.no (Alf P. Steinbach) wrote in news:40****************@news.individual.net: * Dmitry Epstein <mi**********@northwestern.edu.invalid> schriebt: // begin e.h class E { public: template<class T> static void f(); }; // end e.h
What is wrong here? The compiler cannot instantiate the template without having access to the full template definition, which you should simply put in the header file.
Hm, I know that I can compile a template function all by itself, and some sort of code is generated at that point. How does the compiler deal with the absence of a concrete instantiation, and why does it become a problem elsewhere, like in my example?
If a compiler just sees a function /template/ in a TU, with no implicit or
explicit specializations of it, then no code is generated whatsoever. If
there /are/ specializations, only the code for those is generated. (In a
class template, in fact, only member functions actually used somehow
[called, or have their addresses taken] get instantiated, unless you use
explicit instantiation.) Another possibility is to avoid instantiation at the point of usage by instantiating the template somewhere else for the relevant types.
But that restricts usage of the template to those types. This doesn't sound like what I want to do, but how would I instantiate the template "somewhere else"? Can you give an example?
For a class template C defined beginning with:
template<typename T>
class C {
...
};
explicit instantiation would be accomplished with:
template<>
class C<int>;
You can do the same for function templates, or selected member functions of
a class template.
-leor Thanks.
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On 04 Apr 2004 00:48:47 GMT, Dmitry Epstein
<mi**********@northwestern.edu.invalid> wrote: al***@start.no (Alf P. Steinbach) wrote in news:40****************@news.individual.net: * Dmitry Epstein <mi**********@northwestern.edu.invalid> schriebt: // begin e.h class E { public: template<class T> static void f(); }; // end e.h
What is wrong here? The compiler cannot instantiate the template without having access to the full template definition, which you should simply put in the header file.
Hm, I know that I can compile a template function all by itself, and some sort of code is generated at that point. How does the compiler deal with the absence of a concrete instantiation, and why does it become a problem elsewhere, like in my example?
If a compiler just sees a function /template/ in a TU, with no implicit or
explicit specializations of it, then no code is generated whatsoever. If
there /are/ specializations, only the code for those is generated. (In a
class template, in fact, only member functions actually used somehow
[called, or have their addresses taken] get instantiated, unless you use
explicit instantiation.) Another possibility is to avoid instantiation at the point of usage by instantiating the template somewhere else for the relevant types.
But that restricts usage of the template to those types. This doesn't sound like what I want to do, but how would I instantiate the template "somewhere else"? Can you give an example?
For a class template C defined beginning with:
template<typename T>
class C {
...
};
explicit instantiation would be accomplished with:
template<>
class C<int>;
You can do the same for function templates, or selected member functions of
a class template.
-leor Thanks.
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Yoon-Soo Lee |
last post by:
I am using Visual C++ .NET 2003 and running into some linking error from the
following template code.
The error messages is
error LNK2019: unresolved external symbol "class...
|
by: Dmitry Epstein |
last post by:
Here is an example of a problem, which I tried to reduce to its
bare essentials:
// begin test1.cpp
class E {
public:
template<class T> static void f();
};
template<class T> void E::f() {}
|
by: Steven T. Hatton |
last post by:
This may be another question having an obvious answer, but I'm not seeing
it. I'm trying to create a class that derives from
std::valarray<std::string>. I don't need a template, and I haven't come...
|
by: Thomi Richards |
last post by:
Hi,
I'm trying to create a simple stack class using C++ and templates.
Everything works well and good if I amke the class totally inline.
However, as soon as I try to seperate the class into a...
|
by: David Komanek |
last post by:
Hi all,
I am trying to learn more about how to use g++/Cygwin to produce dll
files on WinXP. And I have a problem which at the first look seems to
be an obvious dll-export problem, but I don't...
|
by: Robbie Hatley |
last post by:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote:
> Robbie Hatley wrote:
> >
> > I ran into a problem a few days ago when I added a couple of
> > template functions to one of my personal...
|
by: girays |
last post by:
I have a template class which name is EntityRepository and when I
compile this class I get no error. But when I use this class in a main
method I get LNK2019 linking error. std::map object is used...
|
by: nishit.gupta |
last post by:
I was having a problem with template class memer function definition ,
so i serched the net
and find that template class member fuction definition should be in
header file else compilation
will...
|
by: syang8 |
last post by:
Dear all,
I am trying to design classes with stream support. Basically, I want
the operator << work for the base class and all the derived classes.
If the base class is a template class, and...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |