-1

I am trying to access an array of class instances from two source files, was hoping you could point me in the right direction. Here's roughly what I have so far.

//X.h    
extern Object myObj[5];

//A.cpp
#include X.h

Object myObj[5];

myObj[0].doSomething();
...
myObj[4].doSomething();


//B.cpp   
#include X.h

Object myObj[5];

myObj[0].doSomethingElse();
...
myObj[4].doSomethingElse();

I have entirely no idea if I am along the right lines. Even a phrase or two for me to google would be much appreciated.

GeorgeV
  • 72
  • 1
  • 9
  • Statements such as `myObj[0].doSomething();` can't go outside of functions. But other than that, maybe describe the problems you're having? – juanchopanza May 01 '14 at 21:27
  • I believe you'd end up with multiple definitions of `myObj`. It should be defined only once. – Mark Nunberg May 01 '14 at 21:30
  • 1
    Avoid global/static variables to pass stuff around! Rethink your design thoroghly!! – πάντα ῥεῖ May 01 '14 at 21:31
  • @juanchopanza Those statements are inside functions I was just trying to generalise in a way that didn't involve typing all the code. The problem I'm having is that everything compiles fine, but doesn't run how I want, I believe I have two separate arrays, because when I put `A.cpp` and `B.cpp` together just for testing it works fine. However it's imperative to have them separate due to a lot of other code. What I'm trying to do is get the `myObj.doSomethignElse` that's in `B.cpp` to affect the array declared in `A.cpp` – GeorgeV May 01 '14 at 21:33
  • @mnunberg I do end up with multiple definitions but can't find a way that I Can only define it once where it still compiles – GeorgeV May 01 '14 at 21:35
  • Pick either `A.cpp` or `B.cpp` to "host" the definition. – Mark Nunberg May 01 '14 at 21:36
  • @GeorgeV You need only 1 `extern Object myObj[5]` and 1 `Object myObj[5]`. – Chnossos May 01 '14 at 21:36
  • You should try posting some minimal code that reproduces the problem you care about, not some code which generates *other* problems. – juanchopanza May 01 '14 at 21:38
  • possible duplicate of [The usage of extern in c++](http://stackoverflow.com/questions/15841495/the-usage-of-extern-in-c) – R Sahu May 01 '14 at 21:38
  • @mnunberg @Chnossos when the definition is in only one source file (A.cpp), I get a `variable undefined` error when compiling, pointing to the line in B.cpp where the `myObj.doSomethingElse()` is. – GeorgeV May 01 '14 at 21:39
  • @GeorgeV 'variable undefined' won't happen if you properly #include the extern. Your attempt to show what you have is full of misleading elements - show the real thing. – ScottMcP-MVP May 01 '14 at 21:52

1 Answers1

1

You could get a pointer to the array. so int file "A"

Object arr[5];

Object* GetArr(){
   return &arr;
}

and file "B"

#include "a.cpp"
void DoSomething(){
    Object* pArr = GetArr();
    //use pArr
}

That may work for your situation. Good Luck

Alex Zywicki
  • 2,263
  • 1
  • 19
  • 34
  • Your answer put me on the right track. I ended up with: Object * arr[5] = new Object[5]; Object** GetArr() { return &arr; } then using it like this Object** arrB = GetArr(); – GeorgeV May 04 '14 at 10:26
  • That's the same principle just with one extra layer of pointers. So good, I'm glad I could help . – Alex Zywicki May 04 '14 at 16:46