How can I place lambdas in header files without generating multiply defined symbols from the linker?

Hi,

I need to have definitions (or declarations) of lambdas placed in header files so I can use the lambdas in several cpp source files, but I get multiply defined symbols from the linker...


Any ideas?

Thanks,

Juan
Show us what the problem code looks like.
1
2
3
4
5
6
7
8
9
//file 1: x.h
auto selectViaConcept = [](const Columns::ConceptCol& concept, const std::shared_ptr<StatementLine>& lines)
{
	return lines->m_concept == concept.value;
};

auto selectViaDate = [](const Columns::LineDateCol& dp, const std::shared_ptr<StatementLine>& lines) {
	return lines->m_lineDate == dp.value;
};


1
2
3
// file 2: x.cpp
#include "x.h"
// need to use selectViaConcept in an operation related to concept columns 


1
2
3
// file 3. y.cpp
#include "x.h" 
// need to use selectViaConcept in an operation related to concept columns 


This leads to multiply defined symbols...

Lambdas are objects right? Is there no way of declaring a lambda extern so it can be referenced from several cpps but only defined in one?

Thanks,
Juan
That's not a lambda. A lambda is an unnamed function.

https://en.wikipedia.org/wiki/Anonymous_function

You are using lambda syntax to define functions. Why not just create a standard inline function???
Great reply PanGalactic!!

Thanks a lot for the clarification

Juan
Topic archived. No new replies allowed.