Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template instantiation #7

Closed
jcoupey opened this issue Feb 25, 2021 · 8 comments · Fixed by #8
Closed

Template instantiation #7

jcoupey opened this issue Feb 25, 2021 · 8 comments · Fixed by #8
Assignees
Labels

Comments

@jcoupey
Copy link

jcoupey commented Feb 25, 2021

First of all many thanks for providing this useful C++ implementation!

This is more of a question/comment than a real issue: I'm trying to use the code as a dependency in my project and my own code compiles just fine when building and including polylineencoder.h. But then I'm experiencing errors for all of the used functions at linking stage, e.g.

 undefined reference to `gepaf::PolylineEncoder<6>::decode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

I solved this by explicitly forcing the instantiation for precision values I need. Adding at the end of polylineencoder.cpp:

template class PolylineEncoder<5>;
template class PolylineEncoder<6>;

Is this the expected way to use the project from elsewhere? If so, maybe it would be worth mentioning in the README.

@vahancho
Copy link
Owner

vahancho commented Feb 25, 2021

Hi,

How do you use Pololineencoder? Do you build it as a library and link your application to it, or you build polylineencoder.cpp as a part of your project?

UPD:

The error you see indicates a missing symbol. It might be that you don't build the corresponding .cpp file that contains the function definitions.

@jcoupey
Copy link
Author

jcoupey commented Feb 25, 2021

I simply build polylineencoder.cpp as part of my project (added it to an existing list of cpp files in my makefile) and then include polylineencoder.h where I need it.

All compilations steps are OK, only the linking fails. It makes sense because when building my client file the compiler has an incomplete view of the PolylineEncoder class so it expects to find actual implementation in another object during linking. And upon building polylineencoder.cpp there is no explicit instantiation of PolylineEncoder<5> or PolylineEncoder<6>.

@vahancho
Copy link
Owner

Ah, I see. The problem is that Polylineencoder is a class template and the member functions should be defined in the header file instead of source file. That explains the errors you get. I haven't noticed the problem with the unit tests because they have the following statement:

#include "polylineencoder.cpp"

Ok, thank you for reporting this. I will take care of it.

@vahancho vahancho self-assigned this Feb 25, 2021
@vahancho vahancho added the bug label Feb 25, 2021
@jcoupey
Copy link
Author

jcoupey commented Feb 25, 2021

the member functions should be defined in the header file instead of source file

That would solve the problem with the additional advantage of making it easier to include as header-only from the point of view of a downstream project.

@jcoupey
Copy link
Author

jcoupey commented Feb 26, 2021

Thanks for the quick fix! Just reporting that I updated with the header-only version and it works fine. 👍

@vahancho
Copy link
Owner

Hi Julien,

Thank you once more for reporting this issue and confirming the fix. I hope the code will work good for your project.

Cheers,
Vahan

@jcoupey
Copy link
Author

jcoupey commented Mar 1, 2021

Thanks again for the fix. FYI, the purpose on my side is to toy around with the polyline output of the Valhalla routing engine in a project handling vehicle routing problems: VROOM-Project/vroom#461.

@vahancho
Copy link
Owner

vahancho commented Mar 9, 2021

Thanks for sharing. Good luck with your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants