Link error using pointers to functions with templates.

F

Filipe Valepereiro

I all.

I need to write a function that convert one string into a vector.
This string represent a serialized form of the vector.

So i come up with this piece of code, that compile just fine.
The problem is linking, VC6++ give me this error:

unresolved external symbol "class std::vector<double,class
std::allocator<double> > __cdecl VectorFromStr(class EFAString
&,double (__cdecl*)(class EFAString &))"
(?VectorFromStr@@YA?AV?$vector@NV?$allocator@N@std@@@std@@A
AVEFAString@@P6AN0@Z@Z)

My question is:
Can i pass pointers to template functions?
If i can't how can i circunvent this problem?

Thanks in advance
Filipe

/******** code here *********/
template <class T>
vector<T> VectorFromStr (string& str, T (*convertTo) (string&))
{
vector<T> v (0);

// skip first [vector=
char *data = strdup (str.data () + strlen ("[vector="));

// last ] becomes end of string
*(data + strlen (data) - 1) = 0;

T val;
char *p;

while (data) {
p = strstr (data, ",");

if (p) *p = 0;

v.push_back (convertTo (data));

// go to next value
data = p;

if (data)
data++;
}

return v;
}
 
V

Victor Bazarov

Filipe said:
I need to write a function that convert one string into a vector.
This string represent a serialized form of the vector.

So i come up with this piece of code, that compile just fine.

The piece of code you posted is a template. Unless there is a bad
syntax error in it, there is no reason for the compiler not to let
it compile. What we need to see is the place where you _use_ that
function.
The problem is linking, VC6++ give me this error:

unresolved external symbol "class std::vector<double,class
std::allocator<double> > __cdecl VectorFromStr(class EFAString
&,double (__cdecl*)(class EFAString &))"
(?VectorFromStr@@YA?AV?$vector@NV?$allocator@N@std@@@std@@A
AVEFAString@@P6AN0@Z@Z)

My question is:
Can i pass pointers to template functions?

There is no such thing as pointers to template functions. Pointers
can only point to an instantiation of a template.
If i can't how can i circunvent this problem?

Perhaps you can post the complete code (without extraneous stuff
not related to the problem at hand).

V
 
D

David Hilsee

Filipe Valepereiro said:
I all.

I need to write a function that convert one string into a vector.
This string represent a serialized form of the vector.

So i come up with this piece of code, that compile just fine.
The problem is linking, VC6++ give me this error:

unresolved external symbol "class std::vector<double,class
std::allocator<double> > __cdecl VectorFromStr(class EFAString
&,double (__cdecl*)(class EFAString &))"
(?VectorFromStr@@YA?AV?$vector@NV?$allocator@N@std@@@std@@A
AVEFAString@@P6AN0@Z@Z)

My question is:
Can i pass pointers to template functions?
If i can't how can i circunvent this problem?

Thanks in advance
Filipe

/******** code here *********/
template <class T>
vector<T> VectorFromStr (string& str, T (*convertTo) (string&))
{
vector<T> v (0);

// skip first [vector=
char *data = strdup (str.data () + strlen ("[vector="));

// last ] becomes end of string
*(data + strlen (data) - 1) = 0;

T val;
char *p;

while (data) {
p = strstr (data, ",");

if (p) *p = 0;

v.push_back (convertTo (data));

// go to next value
data = p;

if (data)
data++;
}

return v;
}

I'm going to take a guess and say that this is answered in the FAQ
(http://www.parashift.com/c++-faq-lite/) Section 34 ("Container classes and
templates") question 12("Why can't I separate the definition of my templates
class from it's declaration and put it inside a .cpp file?") and byeond. If
that's not the problem, post more code (preferably somewhat complete).
 
F

Filipe Valepereiro

Thank's guy's.

It's the second time i fall in this error. I've defined the template
in the cpp file, and didn't notice that. No problem now, the code work
just fine :)

Thank's for the help ...
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top