-1
#include <string>
#include <map>

namespace myNamespace
{
    struct MyStruct
    {
        static std::map<int, std::string> idNameMap;
        // some other static properties
    };

    class MyClass
    {
    private:
        void myMethod() {
            std::map<int, std::string>& myMap = MyStruct::idNameMap; // C2062: type 'int' unexpected

            for (auto& it : myMap)
            {
                // do some stuff with map values
            }
        }
    };
}

I'm trying to reference the static map property in MyStruct but it is producing this error. I'm not sure if more context is needed, but if so please let me know.

sookie
  • 2,437
  • 3
  • 29
  • 48
  • Have you #included and , right? – roalz Jan 23 '17 at 14:21
  • @roalz: I do yes. Will add that in for clarification – sookie Jan 23 '17 at 14:23
  • 1
    You should edit your question to provide a minimal, complete and verifiable example: http://stackoverflow.com/help/mcve – roalz Jan 23 '17 at 14:24
  • Is `MyStruct` in the same header as `MyClass`. So we can assume there is no circular include path. – drescherjm Jan 23 '17 at 14:25
  • Possible duplicate of [C++ initialize static variables in class?](http://stackoverflow.com/questions/5019856/c-initialize-static-variables-in-class) – jamek Jan 23 '17 at 14:26
  • 1
    What compiler are you using? I cannot reproduce the error with msvc 18.00 or gcc 6.1 – MadScientist Jan 23 '17 at 14:26
  • @drescherjm: Yeah, same header file – sookie Jan 23 '17 at 14:27
  • @MadScientist: msvc 19.00 – sookie Jan 23 '17 at 14:39
  • Could it be related to the static variables not being initialized properly? – sookie Jan 23 '17 at 14:40
  • ***Could it be related to the static variables not being initialized properly?*** No – drescherjm Jan 23 '17 at 14:44
  • @sookie: either you are not using vs 2015 (vc140, aka msvc 19.00, just tested, compiles this code fine), or you are not compiling the same code you posted. Other possible causes may include evil re-definition of types (typedefs or #defines anywhere?) – roalz Jan 23 '17 at 14:47
  • I just compiled this code in Visual Studio 2015 without any issue at all. I mean after adding a main and defining idNameMap (since this is not a complete example). – drescherjm Jan 23 '17 at 14:48
  • @drescherjm: Right, thanks for taking the time out to help. I'd say a different error is propagating other errors (producing this one) – sookie Jan 23 '17 at 14:51
  • @sookie: Is "C2062: type 'int' unexpected" the exact error message you get? – roalz Jan 23 '17 at 14:54
  • You can easily copy the text of the error message from the Output Tab of Visual Studio after you build. – drescherjm Jan 23 '17 at 14:56
  • @roalz: Yes, along with 100+ other errors (mostly syntax errors which don't seem to point to anything out of the ordinary) – sookie Jan 23 '17 at 15:09
  • See my answer, it should help you in spotting the root cause of the error, hopefully.. – roalz Jan 23 '17 at 15:14
  • @roalz: Yep see it, looking through my code now – sookie Jan 23 '17 at 15:15
  • My advice is to look at the errors in the order that they appear in your source code. Solve the first error first. I find it easier to look at this in the Output Tab. – drescherjm Jan 23 '17 at 15:38
  • @drescherjm: First one is C2062. I usually follow the errors from top to bottom – sookie Jan 23 '17 at 15:41

1 Answers1

0

Let's try again...

You probably have an incorrectly terminated definition somewhere in your code, just before an int variable, that is not included in your question.

I.e. with this addition to your posted code, you will get exactly the same compiler error:

#include <string>
#include <map>

double x = 42.0, // ERROR! incorrectly terminated before an 'int' variable
int a = 0;

namespace myNamespace
{
...
roalz
  • 2,699
  • 3
  • 25
  • 42
  • Ok, well there is no variable declaration terminating with a comma – sookie Jan 23 '17 at 15:35
  • It may not be a comma, this is one of the possible causes, but it should be right before an 'int'. – roalz Jan 23 '17 at 15:49
  • Looks like including stdafx.h after including cpp's header file may have caused the issue – sookie Jan 23 '17 at 16:19
  • I tried to move the #include, but it still compiles fine, at least with a basic auto-made stdafx.h (Win32 console project template). Maybe it's something you include or define in your stdafx.h file... or the other way around, something in your header files messes up stdafx.h stuff. – roalz Jan 23 '17 at 16:28
  • Hmmm.. I'm also using win32 console application, with no modifications to stdafx.h. I assume this isn't an MFC application, as mentioned in here: http://stackoverflow.com/questions/16040689/why-stdfax-h-should-be-the-first-include-on-mfc-applications – sookie Jan 23 '17 at 16:34
  • What if you remove #include "stdafx.h" (and disable pre-compiled headers)? – roalz Jan 23 '17 at 16:37
  • I get the same result as when I'm using precompiled headers and ensuring "stdafx.h" is #included first. So ~100 errors seem to have been fixed, including error C2062. The problem with my OP was that I put the function definition in the .h for simplicity sake so you wouldn't have likely come to that conclusion. All-in-all my guess is that with MyClass.h being included before stdafx, it was assumed that it was already compiled, and it was skipped. This must've caused the compiler to not be able to see the declaration for myMap. – sookie Jan 23 '17 at 16:53