У меня есть следующие 5 файлов: global_vars.h, global_vars.cpp content.h content.cpp main.cpp.Как использовать переменную константы C++ extern для аргумента шаблона в другом файле
global_vars.h
#ifndef global_vars_h
#define global_vars_h
namespace Constants{
extern const unsigned int b;
}
#endif
global_vars.cpp
#include "global_vars.h"
namespace Constants{
extern const unsigned int b(5);
}
content.h
#ifndef CONTENT_H_
#define CONTENT_H_
#include "global_vars.h"
#include <bitset>
struct a{
std::bitset<Constants::b> s;
int a=10;
};
#endif
content.cpp
#include "content.h"
a xVar;
main.cpp
#include "content.h"
int main(){
return 0;
}
я получаю следующие ошибки:
In file included from content.cpp:1:0:
content.h:11:31: error: the value of ‘Constants::b’ is not usable in a constant expression
In file included from content.h:4:0,
from content.cpp:1:
global_vars.h:6:28: note: ‘Constants::b’ was not initialized with a constant expression
extern const unsigned int b;
Я должен использовать Константы :: б отличных content.cpp файлов/.h (для других bitsets), а так как я могу это сделать? Цените помощь.
спасибо
Вы не должны иметь ключевое слово "extern" в global_vars.cpp. Он запрещает компилятору создавать переменную в двоичном файле global_vars.o. Вы получите сообщение об ошибке, указав, что переменная не определена. – iksess