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.

4

2 に答える 2

3

テンプレート定義を .cpp に入れることはできません (できますが、そのコンパイル ユニットでそれらを使用している場合にのみ意味があります)。コンパイル後、テンプレートのインスタンスのみが存在します。

于 2013-03-08T17:32:13.853 に答える
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.

于 2013-03-08T17:32:31.233 に答える