![]() |
|
#1
|
|||
|
|||
Array of ObjectsHello all. I am pretty new to C++ so I hope I can explain my problem. I am trying to create an array of objects. I want to read in a file that contains 1's and 0's and create an object for each 1 or 0. As I create each object I want to place them into an array. I think I have figured out how to read in the file and create the objects but I am having trouble putting them into an array. Please help! Thanks in advance.
Here is my code so far.... CPP / C++ / C Code:
Last edited by LuciWiz : 21-Feb-2006 at 01:04.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
||||
|
||||
Re: Array of ObjectsYou need to convert each byte to an int before it can be stored as an integer.
Firest declare current as a char array because 1 in ascii is 0x31 and 0 is 0x30. Next you want to convert those input bytes respectively to 0x01 and 0x00 and store them in 4 bytes (an integer). to do this i would use the atoi function declared in stdlib.h so it would be: CPP / C++ / C Code:
see a synopsis here: http://man.he.net/man3/atoi Last edited by LuciWiz : 21-Feb-2006 at 01:05.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#3
|
|||
|
|||
Re: Array of Objectsignore this.. lifearray[i][j] = current;
i need to know how to create an array to where i can store the Cell objects... |
|
#4
|
||||
|
||||
Re: Array of ObjectsYou can declare an array of classes just like any other type...
CPP / C++ / C Code:
Last edited by LuciWiz : 21-Feb-2006 at 01:34.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#5
|
|||
|
|||
Re: Array of Objectsdoes this look right??
CPP / C++ / C Code:
are you familiar with the game of life? http://www.bitstorm.org/gameoflife/ this is what i am trying to accomplish. i have already made the game using just the integer values and now i am trying to do it using objects.. how would i be able to use my 'play' function (which plays the game) if i am using objects.. here is the code.. thanks CPP / C++ / C Code:
Last edited by LuciWiz : 21-Feb-2006 at 01:39.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#6
|
|||
|
|||
Re: Array of ObjectsQuote:
CPP / C++ / C Code:
(But you will need another constructor --- with no parameters) CPP / C++ / C Code:
Regards, Dave |
|
#7
|
||||
|
||||
Re: Array of ObjectsYour first code group looks alright. I'm not quite sure what you mean be 'how can i use my play function' though. If you mean how can you pass the Cell object to the function there are several ways to do this. You could declare Cell as a global variable so all functions could access it or you could pass it as a variable to the function. The way I would do it though is to pass it by refrence.
Code:
|
|
#8
|
|||
|
|||
Re: Array of Objectswell my play function is set up to where i send it an array. is there any way i can still send it an array of class objects ??
|
|
#9
|
||||
|
||||
Re: Array of Objectsof course
CPP / C++ / C Code:
Last edited by LuciWiz : 21-Feb-2006 at 01:42.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#10
|
|||
|
|||
Re: Array of Objectsi have to copy the values into a temp array so the final values will not be altered.. when i do this..
CPP / C++ / C Code:
it gives me 2 errors on the line where i try to set the value that is in cellArray to the same spot in the temp array.. it says: 'class Cell' does not define this operator or a conversion to a type acceptable to the predefined operator. and it also says: '.getValue' must have class/struct/union type.. Last edited by LuciWiz : 21-Feb-2006 at 01:45.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 09:25 |
| Merge and Heap...which is really faster | silicon | C++ Forum | 0 | 10-May-2005 13:46 |
| Help with an array using pointers | glulu76 | C++ Forum | 1 | 01-May-2005 11:21 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The