Probably there is a really obvious answer to my question that I can't figure out.
I want to include to my main project a class containing a static member, but i get always a linker error. What I have done so far:
In the same folder I have main.cpp and element.hpp.
Code inside main.cpp:
#include <iostream>
#include "element.hpp"
int main(){
element a(1);
element b(2);
std::cout << a(1) << std::endl;
return 0;
}
and code inside element.hpp:
#ifndef _STATIC_HPP
#define _STATIC_HPP
class element{
private:
static double lambda;
public:
element(double const& l){ lambda = l; }
double operator()(double const& x){ return lambda*x;}
};
#endif //_STATIC_HPP
To execute i use:
$ c++ -std=c++14 -c element.cpp;
$ c++ -std=c++14 main.cpp element.o -o exe
$ ./exe
What I expect to obtain is 2, but instead I get the following error message:
Undefined symbols for architecture x86_64:
"element::lambda", referenced from:
element::operator()(double const&) in main-f8deeb.o
element::element(double const&) in main-f8deeb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [exe] Error 1
I am becoming mad at this! Anyone any Ideas how to fix it?