1

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(); } 
8
  • 3
    Related: #include "anyfile.cpp" is a bad idea no matter what. You #include headers; you compile and link .cpp's. Commented Mar 7, 2014 at 12:39
  • I can't see anything wrong that would cause a multiple definition here. Commented Mar 7, 2014 at 12:40
  • Unless he's compiling all of the cpps... Commented Mar 7, 2014 at 12:40
  • @Salgar that must be it. The problem wouldn't be with the const though (as the title suggests), but with the functions. Commented Mar 7, 2014 at 12:41
  • 2
    @beginh thats because you need to provide a function prototype in a header file, and #include that header Commented Mar 7, 2014 at 12:48

3 Answers 3

4

The problem is that each .cpp includes the .h. This means that each .o contains a const int x. When the linker links these together, you get multiple definitions.

The solution is to modify the .h

#ifndef STORY #define STORY extern const int x; //Do not initialise #endif 

and in a single .cpp:

const int x=4

Edit: I didnt even see the #include <file.cpp> business. Don't do that. Its horrible.

Sign up to request clarification or add additional context in comments.

1 Comment

#include "file.cpp isnt different from #include "file.h". Its just about how these files were intended to be used. Youre of course right that its not good practice, however there ARE some case where including files other than headers is useful.
0

This should be like :

header.h:

#ifndef STORY #define STORY const int x = 4; void func1(); void func2(); #endif 

fun1.cpp

#include <iostream> #include "header.h" void func1() { int w = x; std::cout << "func1 " << w << std::endl; } 

fun2.cpp

#include <iostream> #include "header.h" void func2() { int z = x; std::cout << "func2 " << z << std::endl; } 

main.cpp

#include <iostream> #include "header.h" using namespace std; int main() { func1(); func2(); } 

You can not include ".cpp"

Comments

0

It can be done such as :

header.h:

#ifndef STORY #define STORY const int x = 4; void func1(); void func2(); #endif 

fun1.cpp

#include <iostream> #include "header.h" using namespace std; void func1() { int w = x; cout << "func1 value of w = " << w << "\n"; } 

fun2.cpp

#include <iostream> #include "header.h" using namespace std; void func2() { int z = x; cout << "func2 value of z = " << z << "\n"; } 

main.cpp

#include <iostream> #include "header.h" int main() { func1(); func2(); } 

".cpp" file cannot be included in main source file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.