In one of my classes, I am trying to use std::priority queue
with a specified lambda for comparison:
#pragma once
#include <queue>
#include <vector>
auto compare = [] (const int &a, const int &b) { return a > b; };
class foo
{
public:
foo() { };
~foo() { };
int bar();
private:
std::priority_queue< int, std::vector<int>, decltype(compare)> pq;
};
My program compiles perfectly until I add a .cpp
file to accompany the header:
#include "foo.h"
int foo::bar()
{
return 0;
}
This time, my compiler generates an error:
>main.obj : error LNK2005: "class <lambda> compare" (?compare@@3V<lambda>@@A) already defined in foo.obj
Why can't I create a accompanying .cpp
file if my header file contains a lambda?
Compiler: Visual Studio 2012
My main.cpp
:
#include "foo.h"
int main(){
return 0;
}