-1

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?

Bob
  • 821
  • 1
  • 17
  • 36
  • 3
    I'm more surprised that the *compiler* doesn't complain about the missing semicolon! – Kerrek SB Sep 07 '12 at 07:28
  • 1
    Not to mention the repeated use of the right shift operator. – WhozCraig Sep 07 '12 at 07:31
  • 1
    You sure you're compiling the cpp file? – Luchian Grigore Sep 07 '12 at 07:31
  • That semicolon is there in my code, I just forgot to type it in my example code because I just used the bare essentials. Adding semicolon now. – Bob Sep 07 '12 at 07:31
  • 3
    @Chris that could also mean you've left out something else. First, reduce the code in your environment, then copy-paste it here (if it still has the error). – Luchian Grigore Sep 07 '12 at 07:32
  • 3
    @Chris: Please don't post pseudo-code, memory-code, or napkin-code. Please only post copy-pasted code from the exact test file that produces your error. – Kerrek SB Sep 07 '12 at 07:32
  • @CraigNelson that should work in MSVS2010. – Luchian Grigore Sep 07 '12 at 07:32
  • 2
    I'd suggest not to write `using namespace` in a header file. Anybody who includes that header will also have the using directive in his own code which may cause problems (there's a reason for having namespaces!). It's better to fully qualify names in the header and use the `using` directive only in cpp. – Alexander Tobias Bockstaller Sep 07 '12 at 07:38
  • @LuchianGrigore: It should, but it doesn't on mine (VS2010 SP1). And i t definitely bombs in the llvm c++ compiler on my mac. – WhozCraig Sep 07 '12 at 07:41

1 Answers1

2

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.

evpo
  • 2,436
  • 16
  • 23
  • 1
    If you really want to know what is being searched for those symbols you can also ramp up the linker output for whatever toolset you're using, in VC++ it is pretty trivial on the linker output settings config. whenever you have a problem like this you can/should always turn that up just to get an idea of what is being searched to resolve the ids being sought after. – WhozCraig Sep 07 '12 at 07:51