0

I have written a class inside a file called MyClass.h and it's member functions are located inside a file called MyClass.cpp.
How would I (if possible) access the objects created inside that .cpp file from outside that file?

MyClass.cpp
MyClass* player[] = { nullptr, nullptr}; //The player objects are created using a for loop.
for (i = 0; i < MyClass::createPlayers; ++i) player[i] = new MyClass;

main.cpp

player[i]->getName(); //error: "identifier is undefined"`

MyClass.h
class MyClass { public: MyClass(); //constructor ~MyClass(); //destructor string getName() { return name; }; //returns the name private: string name; //the players' name };

Leten
  • 1
  • 2
  • You just need to link it in to the file you want to use it with. What OS/compiler are you using? – Gillespie May 26 '20 at 16:34
  • `getName` must be metioned in `class.h` in order for it to be visible in other modules. Update your post with your `class.h` contents. Also, I really hope your class isn't really named `class`. – 3Dave May 26 '20 at 16:34
  • @3Dave getName is already mentioned in MyClass.h (see updated code) I also changed the name of the class to MyClass (it has another name in reality) – Leten May 26 '20 at 17:00

0 Answers0