//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
constint 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
constint 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
constint 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.?
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?
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?
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.
externconstint 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'
constint 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.
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.
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.