2

I am getting unresolved externals compile error with following code snippet.

acquire_gray(identity,  []{});

samething happens when I try to use auto

  auto acquire_callback = [](LPBITMAPINFOHEADER pbi, HANDLE hdib)
    {
            printf("Callback\n");
    };

   acquire_gray("",  acquire_callback );

but when I pass in null it compiles

acquire_gray(identity,  NULL);

This is structure of my program

driver.cpp

#include "bridge.h"

void TB_AcquireImagesStart(HANDLE hNamedPipe, const TB_Message request)
{
    acquire_gray("",  []{});
}

bridge.h

template<typename T>
void acquire_gray(const string_t& identity, T& callback);

bridge.cpp

template<typename T>
void acquire_gray(const string_t& identity, T& callback)
{
   callback();
}

So the two exceptions that I am getting are

Error   12  error LNK1120: 1 unresolved externals

Error   11  error LNK2019: unresolved external symbol "void __cdecl acquire_gray<class <lambda_e125ff607fe0339bba6077ce9c14d586> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class <lambda_e125ff607fe0339bba6077ce9c14d586> &)" (??$acquire_gray@V<lambda_e125ff607fe0339bba6077ce9c14d586>@@@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAV<lambda_e125ff607fe0339bba6077ce9c14d586>@@@Z) referenced in function "void __cdecl TB_AcquireImagesStart(void *,class TB_Message)" (?TB_AcquireImagesStart@@YAXPAXVTB_Message@@@Z) 

So my question is what is wrong with my code, and how can I fix this, and why is auto not detecting my lambda type.

Greg
  • 1,671
  • 2
  • 15
  • 30
  • 4
    You cannot put template function definitions in separate `.cpp` files. The compiler won't see them. Try putting the definition of `acquire_gray<>()` in `bridge.h` – Andy Prowl Mar 08 '13 at 17:30

2 Answers2

3

You can't put template definitions in .cpp (well you can, but it only makes sense if you're using them in that compilation unit). After compilation, only template instanciations exist.

Cubic
  • 14,902
  • 5
  • 47
  • 92
2

This is just a common pitfall when using templates. You cannot (or at least should not) separate a template into header (.hpp) and source (.cpp) files. See here for details.

Matt Kline
  • 10,149
  • 7
  • 50
  • 87