+ 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.
7 Antworten
+ 11
Do you have a simple code snippet? We may need an example to try to help you with it.
+ 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;
}
+ 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.
+ 1
Will do thanks again.