2

Can anyone tell me how to access an array declared and defined in a function in another function. I defined class.

class main_menu
{ 
    public:             
        void random_number();
        void search_array();
}; 

Then I defined two functions :

void main_menu :: random_number()
{
    // Size is global variable so I can access to this variable anywhere.
    long random_digit[Size]; 
}

void main_menu :: search_array()
{
   I want to access random_digit[Size]   here.
}  

I wish someone help me. Thanks .

shapiro yaacov
  • 2,308
  • 2
  • 26
  • 39
  • Make `random_digit` a global variable to begin with? – shapiro yaacov Sep 09 '15 at 08:48
  • Make it a member of your class instead. `Size` should probably also be a member instead of a global. – molbdnilo Sep 09 '15 at 08:51
  • Make `random_digit` a member of the class `main_menu`, rather than local to any of its member functions. – Peter Sep 09 '15 at 08:52
  • *"Size is global variable"*, I hope `Size` is `const`, else you use VLA extension which is not standard. – Jarod42 Sep 09 '15 at 08:55
  • I will put my code here. You can download it and check it. I really need to fix it. Maybe I could not explain as well . https://www.dropbox.com/s/rpvefhr9dwuqjtg/Ass1Test.cpp?dl=0 – Mahdi Jafari Sep 09 '15 at 09:18

4 Answers4

1
class main_menu
{
     long random_digit[ Size ];

public:
     void random_number()
     {
         // TODO: fill random_digit array here, e.g. random_digit[ i ] = rand()
     }

     void search_array()
     {
         // TODO: access to random_digit here, e.g. some_expression = some_func( random_digit[ i ] )
     }
};
eraxillan
  • 1,552
  • 1
  • 19
  • 40
  • I will put my code here. You can download it and check it. I really need to fix it. Maybe I could not explain as well . https://www.dropbox.com/s/rpvefhr9dwuqjtg/Ass1Test.cpp?dl=0 – Mahdi Jafari Sep 09 '15 at 09:18
1

Based on the code you provided I recommend that you use a vector because it can be filled with an arbitrary amount of elements. vector is a generic class which will handle arbitrary user defined types as elements (read here to get understanding of generics). The variable has to be declare as member variable. If you declare it inside a method, the variable is only accessible in there and not in other methods. Here is an example how to use the vector in your context:

#include <vector>

class main_menu {
  private:
    std::vector<long> random_digits; 
  public:
    void random_number();
    void search_array();
}; 

In the implementation you fill the vector like this:

using namespace std;

void main_menu :: random_number() {
  int Size = // init it with 10 or 20;

  for(int index = 0; index < Size; index++) { 
    long random = (rand()% Size) + 1; 

    // this line fills the vector
    random_digits.push_back(random);
    cout << random << ' ';  
  } 
  cout << endl;
}

In the other method you can access your elements like using an array:

void main_menu :: search_array() {
    cout << "First element: " << random_digits[0] << endl;
}

Look also at the documentation of vector.

schrieveslaach
  • 1,689
  • 1
  • 15
  • 32
0

random_digit[size] Has scope and lifetime limited to the method main_menu::random_number() this means when that function returns the random_digit[] array cease to exists therefore cannot be used anymore.

A common solution is to put random_digit[] as member variable as suggested by user eraxillan.

Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46
0

Make random_digit member of class main_menu

class main_menu
{

    long random_digit[Size];

    public:

    void random_number();

    void search_array();
}; 
Maciej
  • 9,355
  • 2
  • 15
  • 18
  • I will put my code here. You can download it and check it. I really need to fix it. Maybe I could not explain as well . https://www.dropbox.com/s/rpvefhr9dwuqjtg/Ass1Test.cpp?dl=0 – Mahdi Jafari Sep 09 '15 at 09:18