简体   繁体   中英

How to initialize extern variables in a namespace

I have a global.h that looks like:

#pragma once

#include <memory>

namespace qe
{

    class SubSystemA;
    class SubSystemB;
    class SubSystemC;

    namespace Systems
    {

        extern std::unique_ptr<SubSystemA> gSubSystemA;
        extern std::unique_ptr<SubSystemB> gSubSystemB;
        extern std::unique_ptr<SubSystemC> gSubSystemC;

    }

}

Now I'm not sure if I can initialize it in my main.cpp but w/e I'm doing, it's not working ... please advice. Here's what main looks like:

#include "SubSystemA.h"
#include "SubSystemB.h"
#include "SubSystemC.h"
#include "global.h"

int main()
{

    extern std::unique_ptr<qe::SubSystemA> qe::Systems::gSubSystemA;
    extern std::unique_ptr<qe::SubSystemB> qe::Systems::gSubSystemB;
    extern std::unique_ptr<qe::SubSystemC> qe::Systems::gSubSystemC;

    qe::Systems::gSubSystemA = std::make_unique<qe::SubSystemA>();
    qe::Systems::gSubSystemB = std::make_unique<qe::SubSystemB>();
    qe::Systems::gSubSystemC = std::make_unique<qe::SubSystemC>();

    return 0;
}

Basically I get "unresolved external symbol" error and I'm not sure how I can fix it. Any help is appreciated thanks!

Edit: while solving this is nice to know, alternative suggestions on doing this are welcome. I just want to have easy (doesn't means global but I don't mind it) access to sub system like objects.

You should define(initialize) them out of main() (ie in namespace scope), and don't use extern specifier, which indicates a declaration. eg

std::unique_ptr<qe::SubSystemA> qe::Systems::gSubSystemA = std::make_unique<qe::SubSystemA>();
std::unique_ptr<qe::SubSystemB> qe::Systems::gSubSystemB = std::make_unique<qe::SubSystemB>();
std::unique_ptr<qe::SubSystemC> qe::Systems::gSubSystemC = std::make_unique<qe::SubSystemC>();

int main()
{
    ...
}

The lines

qe::Systems::gSubSystemA = std::make_unique<qe::SubSystemA>();
qe::Systems::gSubSystemB = std::make_unique<qe::SubSystemB>();
qe::Systems::gSubSystemC = std::make_unique<qe::SubSystemC>();

in main don't define the variables. They assign values to the variables. They need to be defined outside all functions. It's best to define them in the .cpp file corresponding to the .h file in which they are declared. In your case, that file should be global.cpp. Add the following lines outside all namespace.

qe::Systems::gSubSystemA = std::make_unique<qe::SubSystemA>();
qe::Systems::gSubSystemB = std::make_unique<qe::SubSystemB>();
qe::Systems::gSubSystemC = std::make_unique<qe::SubSystemC>();

You can also use:

namespace qe
{
   namespace Systems
   {
      gSubSystemA = std::make_unique<SubSystemA>();
      gSubSystemB = std::make_unique<SubSystemB>();
      gSubSystemC = std::make_unique<SubSystemC>();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM