3
votes

I am getting some error message that is saying 'Error: identifier 'XXXX' out of scope' in VS 2012.

I found this still happens with even very simple class as below.

// this is header file
public class IRIS_Database
{
public:
    int test1;
    IRIS_Database::IRIS_Database(void);
    IRIS_Database::~IRIS_Database(void);
};


// this is cpp file
#include "IRIS_Database.h"

/*******************
    Constructor
********************/
IRIS_Database::IRIS_Database(void)
{
    test1 = 5;
    int test2 = 20;

}

/*****************
    Destructor
******************/
IRIS_Database::~IRIS_Database(void)
{
}

I put break point inside of constructor and added test1 and test2 on watch. Here is screenshot.

enter image description here

As you can see image, test1 that is declared in header file is 'out of scope'. The test2, local variable that is declared in constructor, is OK. I can track test2 but not test1.

Here is code that initiate this class from main function.

/*********************
    Main Function
**********************/
int _tmain(int argc, _TCHAR* argv[])
{

    // Initialize Database
    IRIS_Database* IRDB = new IRIS_Database();
}

I am not sure why...

Optimization is disabled in project property and this is running as debug mode. My code is C++/CLI mixed with Windows forms. So, /clr option is enabled. Platform Toolset is v110 which is VS 2012. Here is all options from property page. If you want to see specific options please let me know.

/GS /analyze- /W3 /Zc:wchar_t /I"../IRDB_Include" /Zi /Od /sdl- /Fd"Debug\vc110.pdb" /fp:precise /D "_CRT_SECURE_NO_WARNINGS" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Oy- /clr /FU"C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\mscorlib.dll" /FU"C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Data.dll" /FU"C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.dll" /FU"C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Drawing.dll" /FU"C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Windows.Forms.dll" /FU"C:\Program Files\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Xml.dll" /MDd /Fa"Debug\" /EHa /nologo /Fo"Debug\" /Fp"Debug\NearIR.pch"

Thanks in advance!

1
Thanks Hans, I did update and changed Platform toolset to vs 2010 and it worked.diehard98

1 Answers

2
votes

In this particular case, the instance variable test1 is not yet created. The next line to execute (marked with the yellow arrow) is the first line in the function. It has not yet actually executed, so the path of execution has not quite yet set up the instance and so test1 is not yet available. (Does setting test2 first make a difference?)

On the flip side, test2 is a local variable to the constructor and is stored locally to the function. Thus, it is always accessible anywhere in that function.

This is just a problem with the Watch tool. It takes some extra work, but a good debugger should be able to show that a class's member variables exist within the constructor. MS said that they're working on this issue.