Calling a static class-member template function

O

Olumide

Hi -

I've got a static class member function

MyClass{
public:
template said:
};

template <typename T> void VTK_Writer::converter( list<T > &lst,
vector<T > &vec )
{
// does weird and wonderful stuff
}

It compiles well. However, when I try to call/use this function as
such,

main( )
{
list<int > A;
vector<int > B;
MyClass::converter( A, B ); // E R R O R
}

The Microsoft C++ compiler "rewards" me with the linker error LNK2019.
What am I doing wrong?

Thanks,

- Olumide

PS: is it a static class member function template OR a static class
member template function? ;-)
 
A

Alf P. Steinbach

* Olumide:
Hi -

I've got a static class member function

MyClass{
public:

};

template <typename T> void VTK_Writer::converter( list<T > &lst,
vector<T > &vec )
{
// does weird and wonderful stuff
}

MyClass != VTK_Writer

Also, if this code is in a header file, then it's not a good idea to
have "using namespace std;" in a header file.

If this code is not in a header file, but in a separately compiled
implementation file, then see FAQ item 35.12 "Why can't I separate the
definition of my templates class from it's declaration and put it inside
a .cpp file?" e.g. at <url:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12>.

It compiles well. However, when I try to call/use this function as
such,

main( )

'main' must have result type 'int'.

{
list<int > A;
vector<int > B;
MyClass::converter( A, B ); // E R R O R
}

The Microsoft C++ compiler "rewards" me with the linker error LNK2019.

For the meaning of error codes see your compiler/linker's documentation.

Presumably the linker also spit out a textual message.

It would have been a good idea to reproduce that message, although it's
not difficult to guess that it would involve words like "undefined" or
"missing".

What am I doing wrong?

See above.

PS: is it a static class member function template OR a static class
member template function? ;-)

I don't think it's meaningful to make that distinction (there is a myth
that experts know what is what, but that's just a myth), but for the
definition of the function I'd prefer the first.


Cheers, & hth.,

- Alf
 
O

Olumide

MyClass != VTK_Writer

Oops, I meant to write template <typename T> void
VTK_MyClass ::converter( ... ) . I think project specific names in
code extracts an avoidable distraction.
Also, if this code is in a header file, then it's not a good idea to
have "using namespace std;" in a header file.

Got that.
If this code is not in a header file, but in a separately compiled
implementation file, then see FAQ item 35.12 "Why can't I separate the
definition of my templates class from it's declaration and put it inside
a .cpp file?" e.g. at <url:http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12>.

I've already had a look at this page but I've not found the answer to
this problem. I guess I've got no choice but to keep looking :( .
'main' must have result type 'int'.

This I'm well aware of. Actually I'm writing a plugin, and my code
therefore does not contain a main function.
Presumably the linker also spit out a textual message.

Indeed it does, and here it is (this time I'm not removing project
specifics)

PolygonalCurveOnSurface_writers.obj : error LNK2019: unresolved
external symbol "public: static void __cdecl
VTK_Writer::listToVectorConverter<struct _GtsVertex *>(class
std::list<struct _GtsVertex *,class std::allocator<struct _GtsVertex
*> > &,class std::vector<struct _GtsVertex *,class
std::allocator<struct _GtsVertex *> > &)" (??
$listToVectorConverter@PAU_GtsVertex@@@VTK_Writer@@SAXAAV?
$list@PAU_GtsVertex@@V?$allocator@PAU_GtsVertex@@@std@@@std@@AAV?
$vector@PAU_GtsVertex@@V?$allocator@PAU_GtsVertex@@@std@@@2@@Z)
referenced in function "private: void __thiscall
PolygonalCurveOnSurface::printConvexHullStats(bool)" (?
printConvexHullStats@PolygonalCurveOnSurface@@AAEX_N@Z)
...\PhysicallyBasedFA\Debug\CustomLocator.mll : fatal error LNK1120: 1
unresolved externals
 
O

Olumide

Oops, I meant to write template <typename T> void
VTK_MyClass ::converter( ... ) . I think project specific names in
code extracts an avoidable distraction.

Gah! ... Third time lucky -- MyClass ::converter( ... )
 
J

James Kanze

* Olumide:

[...]
I don't think it's meaningful to make that distinction (there is a myth
that experts know what is what, but that's just a myth), but for the
definition of the function I'd prefer the first.

The "experts" (i.e. the members of the standards committee) have
decided that within the scope of the standard, there is no such
thing as a "template function", only a "function template", with
its various instantiations and specializations. I think that
part of the motivation for this was that they didn't know
exactly what "template function" should mean.

In usual, everyday English, in such expressions, the first word
is an adjective, or is used as such, and the second is a noun.
Since what we have in the declaration is not a function, but a
template, "function template" makes more sense, but that's just
general English usage, not specialized, technical English.

I sort of prefer this use as well, since it reminds you that the
declaration does not define a function, but rather a template
(whose specializations and instantiations will be functions).
There are a few cases where it is important to keep this in
mind.

Now, if only someone could explain to me the exact difference
between a specialization and an instantiation.
 
J

James Kanze

I've got a static class member function
MyClass{
public:


template <typename T> void VTK_Writer::converter( list<T > &lst,
vector<T > &vec )
{
// does weird and wonderful stuff
}
It compiles well. However, when I try to call/use this function as
such,
main( )
{
list<int > A;
vector<int > B;
MyClass::converter( A, B ); // E R R O R
}
The Microsoft C++ compiler "rewards" me with the linker error
LNK2019. What am I doing wrong?

The obvious answer that people like to give around here is to
quote the FAQ where it says, give us an exact, compilable
example which reproduces the problem. In this case, it's
possible with some exterpolation (e.g. that the error LNK2019
means "unresolved external") to guess that maybe you've put the
function definition in a source file, rather than include it in
the translation unit in which it is used. VC++, like a number
of other compilers, will not find it in this case, and the
linker will complain.

But as I said, it's just a guess. (It just happens to be such a
frequent error that one assumes it in such cases.)
 
J

Jerry Coffin

[ ... ]
Now, if only someone could explain to me the exact difference
between a specialization and an instantiation.

14.8/1: "A function instantiated from a function template is called a
function template specialization; so is an explicit specialization of a
function template."
 
M

murali.desikan

Indeed it does, and here it is (this time I'm not removing project
specifics)

PolygonalCurveOnSurface_writers.obj : error LNK2019: unresolved
external symbol "public: static void __cdecl
VTK_Writer::listToVectorConverter<struct _GtsVertex *>(class
std::list<struct _GtsVertex *,class std::allocator<struct _GtsVertex
*> > &,class std::vector<struct _GtsVertex *,class
std::allocator<struct _GtsVertex *> > &)" (??
$listToVectorConverter@PAU_GtsVertex@@@VTK_Writer@@SAXAAV?
$list@PAU_GtsVertex@@V?$allocator@PAU_GtsVertex@@@std@@@std@@AAV?
$vector@PAU_GtsVertex@@V?$allocator@PAU_GtsVertex@@@std@@@2@@Z)
referenced in function "private: void __thiscall
PolygonalCurveOnSurface::printConvexHullStats(bool)" (?
printConvexHullStats@PolygonalCurveOnSurface@@AAEX_N@Z)
..\PhysicallyBasedFA\Debug\CustomLocator.mll : fatal error LNK1120: 1
unresolved externals

Your program does not contain the definition for the function
VTK_Writer::listToVectorConverter as reported by the linker. You have
to define this function.

Thanks,
Murali
 
O

Olumide

Your program does not contain the definition for the function
VTK_Writer::listToVectorConverter as reported by the linker. You have
to define this function.

It did, but it was in the .cpp file and not the .h file. And when I
moved the definition to the .h file, it worked :-| .... (in a small
voice: I still don't like the idea of definitions in the .h file ).
 
M

murali.desikan

It did, but it was in the .cpp file and not the .h file. And when I
moved the definition to the .h file, it worked :-| .... (in a small
voice: I still don't like the idea of definitions in the .h file ).

Please check the questions 35.12 to 35.14 in the following link for
more information on how to avoid linker errors when using template
functions.

http://www.parashift.com/c++-faq-lite/templates.html

Thanks,
Murali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top