(c++) Object access throughout program?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

(c++) Object access throughout program??

I'm making a simple battle simulator. It works fine as is but I'm trying to convert it to OOP style. Problem: I'm trying to access a particular instance in multiple functions and getting weird output. For instance: I created *one* instance of my enemy class at the beggining of main. I'm trying to retrieve enemy stats from this instance in outlying functions. I figured -create an enemy class, then create an object of enemy class to be handled in the battle sequence. I'm new to OOP.

4th Apr 2017, 4:53 AM
Collin Heritage
Collin Heritage - avatar
4 Answers
+ 11
Do you have a simple code snippet? We may need an example to try to help you with it.
4th Apr 2017, 6:07 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
It's public, I think I'm just using it wrong. I did not use a constructor. Here's an example code. #include<iostream> using namespace std; void Function_Test(); class Test(){ public: int number;}; int main(){ Test obj; Function_Test(); return 0;} void Function_Test(){ obj.number = 2; cout<<obj.number; }
4th Apr 2017, 4:19 PM
Collin Heritage
Collin Heritage - avatar
+ 1
That right there IS the problem. My inability to use one particular object in functions that are outside of Main. It makes organization a nightmare. I need a global object if that exists. Thanks for your time.
4th Apr 2017, 4:43 PM
Collin Heritage
Collin Heritage - avatar
+ 1
Will do thanks again.
4th Apr 2017, 4:49 PM
Collin Heritage
Collin Heritage - avatar