error LNK2019: unresolved external symbol "public void _thiscall ...

Hi,

I have my code as shown

Example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Helpers.h"
class Example
{
public:

Helpers helper;
string name= "abc";
void test()
{
  string stringVals[3][2]= {"compare", "two", "name1", "abc", 
                                "def", "123e"}
  helper.compare(name, stringVals);
}
};


Helpers.cpp
1
2
3
4
5
template <size_t N>
void compare(string name, string stringVals[][N]){

do some processing using stringVals and name
}


Helpers.h
1
2
3
4
5
6
class Helpers
{
public:
template <size_t N>
void compare(string name, string stringVals[][N]);
};


I get an error when I build this:
error LNK2019: unresolved external symbol "public void _thiscall Helpers::compareValues<2>(class ATL::

Could someone please explain me why do I get this error when I use template<size_t N> and have compare function in the Helpers.cpp

I donot get this error when I have compare function defined in Example.cpp


Regards
Last edited on
When using templates the code is generated at compile time.
So the compiler needs to know the type that you want to use.

As it doesn't see it, nothing is generated and the linking fails.

So, put the function definition in the header.
Or make an explicit instantiation.
Last edited on
Thank you!

Could you please help me in using explicit instantiation. I would not like to have function definition in header file.

Regards
I don't like to put the function definitions in the header either, but, unfortunately, it's usually necessary with template classes. I've never used explicit instantiation (nothing wrong with it--I've just never needed it), so I am not sure what the syntax is for using it.

What I do, however, is create a ".tcpp" file with all of the template code that would go into a .cpp file if it weren't templated. Then I include the .tcpp file at the end of the header file. This satisfies the compiler, and fits within my sensibilities of what I like to see in a header file. And the .tcpp extension lets me know that it contains template code that cannot be compiled directly. So, something like this:

Helper.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef Helpers_h
#define Helpers_h

class Helpers
{
    public:
    template <size_t N>
    void compareValues_1(string name, string stringVals[][N]);
};

#include "Helper.tcpp

#endif 


Helper.tcpp:

1
2
3
4
5
6
#include "Helper.h"   // Not needed, but it helps the file feel like a .cpp file

template <size_t N>
void compare(string name, string stringVals[][N]){
    do some processing using stringVals and name
}
Thank you!
I can give this a try.

because I'm unable to do explicit instantiation, I would like to also see the code that uses explicit instantiation.
The about case I have can be taken as an example

Last edited on
IIRC
1
2
3
4
5
6
7
8
//Helpers.cpp
template <size_t N>
void compare(string name, string stringVals[][N]){
//...
}

template void compare(string name, string stringVals[][2]);
template void compare(string name, string stringVals[][42]);
Of course you would only have the definition of those explicit instantiated.

You may want to look for an alternative to multidimensional arrays instead. By instance using std::vector, or just unidimensional arrays.

I would not like to have function definition in header file.
¿why not?

Edit: using #include "Helper_definition.hpp" is the same as putting the definition in the header.
Last edited on
Hi,

That is because the function definition is not as simple as I have explained here.

Its a quite big function :)
Last edited on
And also .. I have tried using std:vector but assigning values to 2D vector doesn't seem to be as easy and simple as it is for a multidimentional arrays or am I wrong?
Topic archived. No new replies allowed.