How do I create a global instance of an object in visual C++

Ok what I am doing is creating an objct and filling it with data from a file.  But when that function finishes the object is out of scope.  In c++ I would have just made an instace of that object at the top of my main{} in my main.cpp and pointed to it from my function also in my main.cpp.  In visual c++  there is no main.cpp (or at least I cant figure out how to access it if I do make one). So where would I declare an instance of the class  that I do not want to go out of scope? Or is there a specific way I have to do this?  Thank you for any help you can give!
RackboyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
JustOneFile.cpp
------------------
// some includes
#include "MyObject.h"      <--- define CMyObject class

CMyObject myInstance;


AnyOtherFile.cpp
-------------------
// some includes
#include "MyObject.h"      <--- define CMyObject class

extern CMyObject myInstance;
0
 
Jaime OlivaresSoftware ArchitectCommented:
> In visual c++  there is no main.cpp (or at least I cant figure out how to access it if I make one).
In VC++ you can consider your main file where is located main(), WinMain() or your CWinApp derived object implementation.
But this doesn't matters. You can declare your global object where you consider is related (at the top of a cpp file).
If you want to use it from another cpp file just declare it as external at the top of this file.

extern CMyObject myInstance;
0
 
RackboyAuthor Commented:
Do I put it in the .h or the .cpp?
when I try and access it from the .h file that I put it in I get this error
LINK : error LNK2020: unresolved token (0A000019) punchPro
LINK : fatal error LNK1120: 1 unresolved externals
0
 
Jaime OlivaresSoftware ArchitectCommented:
No, as I said, you must put it at the top of the cpp, but after the #include that declares it.
0
All Courses

From novice to tech pro — start learning today.