I am trying to use the constant int SIZE that is declared in the TaxConstants.hpp namespace TAXCONSTANTS in other places in my project. When I try to compile, I get "undefined reference to 'SIZE' everywhere that SIZE is referenced.
file TaxConstants.hpp
#ifndef TaxConstants_hpp
#define TaxConstants_hpp
namespace TAXCONSTANTS
{
extern const int SIZE = 4; // I have tried with and without extern
}
#endif //TAXCONSTANTS_HPP
main.cpp
#include <iostream>
#include "TaxConstants.hpp"
using namespace std;
using namespace TAXCONSTANTS;
int main()
{
extern const int SIZE;
// This is a struct defined in another file. It is a sample of my use for SIZE. I left out the #include above to simplify things.
taxPayer payers[SIZE];
//More code
return 0;
}
Additional info: this is a school project and my teach has made it a requirement to declare constants in the file TaxConstants.hpp in the namespace TAXCONSTANTS.
There are 5 files in total, the file with my functions is having the same undefined reference to SIZE error.
I have spent hours looking up similar explanations on the extern function and namespaces, but most of the suggestions are against doing this in the first place an offer another solution. I, unfortunately can not use them. Other errors people had were getting "multiple decorations" which I am not having.
EDIT
See Brians explanation below for better detail.
What I needed to do was define
const int SIZE = 4;
within the TaxConstants.hpp file in namespace TAXCONSTANTS.
Then remove 'extern const int SIZE;' from my main file and instead reference SIZE by TAXCONSTANTS::SIZE everywhere I wanted to use size.
This is basic namespace stuff that I completely forgot about.