Multiple .cpp and .h files with constants

Hello, How can I define a constant which can be accessed to any .h or .cpp file in the project? My efforts are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//MAIN.CPP
#include "FILE1.h"
#include "FILE2.h"

//const int a = 100;

int main() { //code }

//FILE1.h

void foo1(int [a]); //here Eclipse shows "Undefined CONSTANT 'a' error".

//FILE1.cpp

const int a = 100; //I cannot use a without defining.
void foo1(int arr[a]) { //code }

//FILE2.h

void foo2(int arr2[a]); //same error as above.

//FILE2.cpp

const int a = 100; //I cannot use a without defining.const int a = 100;
void foo2(int arr2[a]){ //code } 


EDIT 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
//FILE1.h

//if I do something like const int a = 100; in header file.
//Eclipse shows error "already defined constant"
//If I don't define, it gives undefined error.

void foo1(int [a]); //here Eclipse shows "Undefined CONSTANT 'a' error".

//FILE1.cpp

const int a = 100; //I cannot use a without defining it again.
void foo1(int arr[a]) { //code }


Please tell me how can I define a constant single time and use it in every file
i.e. all .cpp files and .h files.?
Last edited on
Put const int a = 100; in a header file and include it in the files that uses a.
Put const int a = 100; in a header file and include it in the files that uses a.


With due respect, what do you mean by include it in the files that uses a.

Do I have redefine it constant there too?
Problem is solved by some hit and trials and somehow Peter87's help.

SOLUTION:

1. I replaced const int a = 100; by #define a 100
2. placed it in all header files.
3. removed(if any declarations of such constants in .cpp files)

SOLUTION WORKED ONLY FOR #DEFINE SYNTAX
DOES NOT WORK FOR const int VARIABLE = VALUE;

Can anyone tell why?
with Peter87s solution , it should also work with const int variable = value;
Last edited on
what do you mean by include it in the files that uses a.

I meant you should #include the header that defines a.

1
2
3
4
5
6
7
// header.h
#ifndef HEADER_H
#define HEADER_H

const int a = 100;

#endif 

1
2
3
4
5
6
7
8
9
// source.cpp
#include "header.h"
#include <iostream>

void foo()
{
	// a is used here
	std::cout << a;
}
yes, thank you all esp Pete87.
SOLUTION WORKED ONLY FOR #DEFINE SYNTAX
DOES NOT WORK FOR const int VARIABLE = VALUE;

Can anyone tell why?


It certainly should work. In C++, a const variable defined at file scope has internal linkage only, i.e. it's only visible in that translation unit. Defining it in a header file should therefore not cause any errors about multiple definitions.

What compiler are you using? Is it a C compiler, or a C++ compiler?
you can create a namespace with a .h and ( .cpp ) , use #ifndef...#endif , then include this header where its needed , it should works.
Ah, wait... OP, are you using multiple header inclusion guards, as Eric alludes to? Is it possible you're including the same header twice in the same translation unit?
@OP, you are getting some bad advice.

Include files exist to tell .cpp files what kinds of things exist in other .cpp files.

Things to not do in a header file:
- const int foo = 12;
- #define foo 12

The correct way:

foozle.hpp
1
2
3
4
5
6
7
8
9
10
#ifndef FOOZLE_HPP
#define FOOZLE_HPP

// A constant integer value named 'foo' exists.
extern const int foo;

// A function named 'fooey' exists.
void fooey();

#endif 

foozle.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "foozle.hpp"

// Here is 'foo'
const int foo = 12;

// Here is 'fooey'
void fooey()
{
  std::cout << foo << "\n";
}

That is a complete module, named 'foozle', with both code (.cpp file) and a public interface (.hpp file) that can be used by other source files (.cpp files).

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "foozle.hpp"

int main()
{
  // I can use the function 'fooey' (because "foozle.hpp" tells me it exists)
  fooey();

  // I can use the constant 'foo' (because "foozle.hpp" tells me it exists)
  std::cout << "foo + 1 = " << (foo + 1) << "\n";
}

Another consideration is that you should wrap everything in a namespace.

For more reading about using multiple files, see the FAQ:
http://www.cplusplus.com/faq/beginners/multiple-sources/

Hope this helps.
But if you use extern const int in the header you will not be able to use the constant as a constexpr.
Last edited on
for me , I assign const in the header and non-const in the cpp when I use namespace .
Include files exist to tell .cpp files what kinds of things exist in other .cpp files.

Header files exist to tell .cpp files everything it's useful for them to know to use other .cpp files. That can very well include the definitions of constants, if it's appropriate to do so.

In any case, no-one's giving the OP advice that it's the right thing to do in this particular case; we're assuming the OP knows his/her mind and has valid reasons for wanting to do this. We're simply trying to resolve issues of C++ syntax, and why something isn't working, that should be.
Peter87 wrote:
But then you can't do something completely different.

Yes, you could just declare it constexpr and stick it in the header.

foozle.hpp
1
2
3
4
5
6
#ifndef FOOZLE_HPP
#define FOOZLE_HPP

constexpr int foo = 12;

#endif 

main.cpp
1
2
3
4
5
6
7
#include <iostream>
#include "foozle.hpp"

int main()
{
  std::cout << "foo + 1 = " << (foo + 1) << "\n";
}

That doesn't undo the bad advice given before constexpr was even mentioned.

Ericool wrote:
...

Always/never rules are bad advice.
Duoas wrote:
That doesn't undo the bad advice given before constexpr was even mentioned.

The constant would be a constexpr even if const was used because the initialization value is constexpr, but it's better to be explicit, I know. :) I guess I should get used to using the constexpr keyword if that is what I intend.
Last edited on
Topic archived. No new replies allowed.