I want to test defining a const in a header and use it in functions, then call it. However I get the error, I added include guards which doesn't help. Error is: LNK1169: One or more defined multiply symbols found. How can i do it in a nother way? Is declaring const in .h and defining this const in .cpp and then including this .cpp in all other .cpps the only solution?
Header
#ifndef STORY #define STORY const int x = 4; #endif .cpp
#include <iostream> #include "8-04.h" void func1() { int w = x; std::cout << "func1 " << w << std::endl; } .cpp
#include <iostream> #include "8-04.h" void func2() { int z = x; std::cout << "func2 " << z << std::endl; } main
#include <iostream> #include "8-04.h" #include "8-04first.cpp" #include "8-04second.cpp" using namespace std; int main() { func1(); func2(); }
#include "anyfile.cpp"is a bad idea no matter what. You#includeheaders; you compile and link .cpp's.