D
D
Daniil Demidko2016-02-24 12:36:41
C++ / C#
Daniil Demidko, 2016-02-24 12:36:41

External constant in header?

Why is it not possible to use extern const in headers if the header itself is not included in the .cpp file?
For example:
MyVar.h
extern const int AnyVar ;
MyVar.cpp
const int AnyVar = 475 ;
main.cpp

#include"MyVar.h"
int main()
{
     int test = AnyVar ; // ошибка
}

But if I fix MyVar.cpp by adding #include"MyVar.h", then everything works fine.
Moreover, if an external non-constant variable is declared in the header, then you don’t have to do this (connect the header to the .cpp implementation).
Why does this happen with constants?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Fedoryan, 2016-02-24
@AnnTHony

Maybe it will help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question