简体   繁体   中英

C++ Class Static Members

So I have my header file with static members:

#ifndef PROFILE_MANAGER_H
#define PROFILE_MANAGER_H

#include <map>
#include <vector>

using namespace std;
using std::vector;

namespace Engine
{
    class ProfileManager
    {
    private:
        static map<const char*, vector<float>> profiles;
        static map<const char*, vector<float>>::iterator it;
        static pair<map<const char*, vector<float>>::iterator, bool> ret;
    };
}

#endif

And in my cpp file i have the definitions:

#include "ProfileManager.h"

namespace Engine
{
    map<const char*, vector<float>> ProfileManager::profiles;
    map<const char*, vector<float>>::iterator ProfileManager::it;
    pair<map<const char*, vector<float>>::iterator, bool> ProfileManager::ret;
}

The linker always complains about the static members being unresolved externals (LNK2001) even though I have defined them in the cpp file. Any ideas as to why?

These kind of errors usually happen when the linker is not given the obj file that is the result of the compilation of cpp.

Look for ProfileManager.obj in your output directory. If it doesn't exist, there is something wrong. Possibly the cpp file is not compiled as Luchian Grigore suggested. It's also possible that the linker is not given the obj file in the parameters. If you are using visual studio, check that the cpp file is a part of the project. In other environments see the command the linker is invoked with.

If you use Visual Studio, you can open the project properties -> Linker -> Command Line and add /VERBOSE in Additional Option. Then open you output window and recompile the project. (Thank you, Craig for the comment).

One more scenario that could have happened. You included the header file in another project and you tried to build without referencing the project where ProfileManager.cpp was.

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