Getting Array from Another class C++

KingJohnno

I am making a simple Lottery program - and am struggling with implementation. I have a class called 'Ticket Line' this class simply holds 6 numbers that the player is playing a lottery for.

What I want to do, is generate 6 randomly (got function for this already) and then store that in another class as values. To do this, I am using the following code:

  class Draw
{
private:
    int drawID; 
    TicketLine* DrawnNumbers;
    bool drawn; 
}

When a Draw is completed I want to generate the Random Numbers ( from the TicketLine class) and then for the Draw to be able to store those numbers into its Draw File.

How would I be able to access the functionality of the DrawnNumbers class - and store the results from the getTicketNumbers.getTicketLine()function.

int* getTicketNumbers(void) { return DrawnNumbers->getTicketLine();}; 

The program crashes the following code:

//int *ptr[6] = getTicketNumbers();
int *ptr[6] = getTicketNumbers();
for (int x = 0; x < 6; x++){
    cout << ptr[x];
}

TicketLine class:

private: 
    int select[6]; //Array of Ticket Numbers.
int* getTicketLine(void) { return select; }; 

I am willing to offer a couple of virtual beers to the solution. I am as yet to find a good online pub - if you know of one then please do let me know :-)

Thanks,

AndyG

Without knowing any more, this line:

int *ptr[6] = getTicketNumbers();

is very suspect.

Why? Well, we haven't seen the implementation of getTicketNumbers so we don't know if it's actually allocating memory for 6 and returning such an array.

Also, you are printing the values of pointers here:

for (int x = 0; x < 6; x++){
    cout << ptr[x];
}

Where, if you intended to actually print the int values, you'd say something like this:

for (int x = 0; x < 6; x++){
    cout << *(ptr[x]);
}

My guess is that you are either:

  • Going out of bounds of an array that was (not) allocated, or,
  • Modifying actual pointer values somewhere instead of the integers they point to (as indicated by your lack of dereferencing ptr[x] in your print statement)

Edit

With more information, it seems you probably meant to say this:

int *ptr = getTicketNumbers();

instead of this:

int *ptr[6] = getTicketNumbers();

You should probably be doing some sanity checks as well to make sure that select is actually filled before calling that function (maybe giving it a default value of {0,0,0,0,0,0} in the constructor)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting elements from array by class

From Dev

Getting value from one activity to another class

From Dev

Getting string from another class and transfering it to url

From Dev

Getting value from GUI and using it in another class

From Dev

Getting TextBox values from another class

From Dev

Getting Error in Accessing Method from another class

From Dev

Stuck at getting array from another thread

From Dev

c# getting a string from textbox using get set , than sending it another class?

From Dev

Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

From Dev

c# getting a string from textbox using get set , than sending it another class?

From Dev

Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

From Dev

Getting the type of another nested class from the same "parent" class

From Dev

Getting same class instance as attribute from another class

From Dev

C++ Class initialization within another class, getting error

From Dev

Getting an array from the redux store puts it inside another array

From Dev

How to access an array from another class

From Dev

How to edit an array from another class in Java

From Dev

pass integer array from one class to another

From Dev

Updating an array from another class Java

From Dev

Java: Import array of Strings from another class

From Dev

Add an element from another class to an array list

From Dev

Accessing a public byte[] array from another class

From Dev

How to get an array from another class

From Dev

get an array from another class in java

From Dev

calling static array from another class

From Dev

Java: Import array of Strings from another class

From Dev

Adding elements to array from another class

From Dev

Making an array object from another Class in Java

From Dev

Storing array from another class in java

Related Related

  1. 1

    Getting elements from array by class

  2. 2

    Getting value from one activity to another class

  3. 3

    Getting string from another class and transfering it to url

  4. 4

    Getting value from GUI and using it in another class

  5. 5

    Getting TextBox values from another class

  6. 6

    Getting Error in Accessing Method from another class

  7. 7

    Stuck at getting array from another thread

  8. 8

    c# getting a string from textbox using get set , than sending it another class?

  9. 9

    Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

  10. 10

    c# getting a string from textbox using get set , than sending it another class?

  11. 11

    Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)

  12. 12

    Getting the type of another nested class from the same "parent" class

  13. 13

    Getting same class instance as attribute from another class

  14. 14

    C++ Class initialization within another class, getting error

  15. 15

    Getting an array from the redux store puts it inside another array

  16. 16

    How to access an array from another class

  17. 17

    How to edit an array from another class in Java

  18. 18

    pass integer array from one class to another

  19. 19

    Updating an array from another class Java

  20. 20

    Java: Import array of Strings from another class

  21. 21

    Add an element from another class to an array list

  22. 22

    Accessing a public byte[] array from another class

  23. 23

    How to get an array from another class

  24. 24

    get an array from another class in java

  25. 25

    calling static array from another class

  26. 26

    Java: Import array of Strings from another class

  27. 27

    Adding elements to array from another class

  28. 28

    Making an array object from another Class in Java

  29. 29

    Storing array from another class in java

HotTag

Archive