Jump to content

  • Log In with Google      Sign In   
  • Create Account


accessing data from other .cpp files


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 19 October 2012 - 10:14 AM

I am working on a text-based RPG and I defined a vector in my game.cpp file and now I am trying to access it from my inputSystem.cpp file, it shows that it is unknown? I am wondering how I can access this vector from this other .cpp files?

-Thanks

Sponsor:

#2 greenvertex   Members   -  Reputation: 510

Like
0Likes
Like

Posted 19 October 2012 - 10:40 AM

See the extern keyword.

#3 Nikko_Bertoa   Members   -  Reputation: 370

Like
0Likes
Like

Posted 19 October 2012 - 10:43 AM

If your vector in game.cpp is

std::vector<someType> someName;

you should write in inputSystem.cpp

extern std::vector<someType> someName;


#4 Bregma   Crossbones+   -  Reputation: 4741

Like
2Likes
Like

Posted 19 October 2012 - 10:55 AM

Better yet, put the extern declaration in a header, and include that header in both .cpp files. That way, if either the declaration (in the header) or the definition (in game.cpp) gets changed bit the other does not, you get a compile failure instead of a mysterious runtime failure.

Also, putting shared variable declarations in header files documents them as being shared between .cpp files.
Stephen M. Webb
Professional Free Software Developer




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS