|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Question about sending multi-dimentional arrays in C++
Here's the gist. I have a 2-dimentional array in my .cpp file, and I want to send it to my .h file. I just want the information there so I can use it, and I can't figure out how to do it. Any help is appreciated.
|
|
#2
|
|||
|
|||
you want to "send" it to your .h file? what is this supposed to be?!?! you want to use an array in your .h file? dude, you didnīt understand the concept - it works the other way around! you "use" the arrays in your .cpp file that you have defined in your .h files...
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
|||
|
|||
|
Perhaps I'll be more specific on why I need to do this.
I'm creating a .h file with a method in it called menu(). it recieves a string that tells it what to put at the top, and an 2-dimentional array for all of the items of the menu. Then it does a cool thing using the up and down arrows that I've already tested and it works. I'm not making this for one individual program. I'm making this to put in my include library so that I can use it to no end. I need to be able to create the array in the .cpp file and have the .h file recieve it so that it can make the menu. Is that more clear?\ (All of this is in dos, in case that is where the mix up is) |
|
#4
|
|||
|
|||
|
it is more clear, but again: you are mis-using the concept.
.h files are for describing abstract functions, variables, classes and macros (what did i forget?). you would only very very seldomly put actual function code there. the .cpp is for implementing the actual code. for building a library, you would: - define the interface in a .h file - implement it in a .cpp file - compile it to a .o file - reuse the .o file, importing it into other projects using the .h file. additionally, for being more professional than re-using the object file, you would build a library of it. |
|
#5
|
|||
|
|||
|
I'm sorry, but I'm lost.
I always put useful functions in .h files so that I don't have to rewrite it. (99 cases out of 100, inside a class). I have yet to hear someone else say not to put functions in a C++ .h file. Examples: pause() cls() (clears the screen) bubblesort() All of which are in .h files and all of which are dead useful. menu() is one problem away from joining them. I don't understand why you wouldn't put methods in .h files. I'm just trying to understand what you're saying here. |
|
#6
|
|||
|
|||
|
i tried to explain that in my last post.
c++ is not that strictly defined. you could put all your functions in a ".txt" file and compile this one. but itīs still no good style. you could #define all your functions, but itīs still no good style you could make a library inside a .h file, but itīs still no good style. Do you get my point? just because it works, it is not automatically right... I probably wonīt reply here anymore, iīll leave this to the other C++ veterans here. Maybe they have some better way to explain why not to do this. Sorry, I am not that deep into the theory ![]() |
|
#7
|
|||
|
|||
|
I'm thinking that would be a good idea as well, because this isn't getting anywhere. I just see this code:
#include <gary.h> ... pause(); as a very effective way of doing things, and in this case, to pause the screen. I'll just come back later when others have replied, and hopefully I'll see an answer. |
|
#8
|
||||
|
||||
|
perhaps i am wrong in my understanding of this problem, but it seems as tho all u want is a function that accepts an array as an argument??
make menu() be menu(char [], int arraySize) simple as that i believe? |
|
#9
|
|||
|
|||
|
For one dimentional arrays, yes, that works. For two dimentional arrays, it doesn't.
|
|
#10
|
||||
|
||||
|
See if the C-FAQ at http://www.eskimo.com/~scs/C-faq/q6.20.html answers your question.
[edit] Also http://www.eskimo.com/~scs/C-faq/s6.html and be sure to read sections 6.18 and 6.19 as well [/edit] As to whether you should put code in .h files or not, M. Hirsch is entirely correct. By convention, code goes into .c files and function prototypes, structure definitions, #defines, external declarations etc should go into the .h file. There are also some very good reasons why this is so (it's not just a convention). 1. It is impossible to share a global variable among several source files in a project, if you put the declaration in the .h file 2. Code will compile slower, since the code in the .h file will be included by the preprocessor and compiled each time. 3. Impossible for you to build your own library of precompiled functions if you put code in the .h file. See http://www.eskimo.com/~scs/C-faq/q10.6.html for more info. Last edited by Scorpions4ever : February 8th, 2003 at 05:26 PM. |
|
#11
|
||||
|
||||
|
oh sorry didnt see it was 2 dimensional.
but from the link above, it seems as though u would pass a 2 dimensional array nearly the same as 1 dimensional. and like they said, the implementation should be separate from the definition. the only exception to this is when making Template classes as it wont compile correctly if the implementation/definition are separate, all definitions should be in the .h file. i believe i am right about this, at least that's how i was taught... do u agree with this hirsch and scorpion? Last edited by infamous41md : February 8th, 2003 at 05:27 PM. |
|
#12
|
|||
|
|||
|
Thankyou very much. It works. 5 lines to make a menu with infinite items on my computer.
Just for the heck of it, seeing as this is probably my last post on this board: 1. You don't want to use global variables. It gets too difficult to keep track of them in large programs. 2. I don't care about a .00001 and a .00002 milisecond difference. Code efficiency is what I care about. 3. By definition, everything in the include library is a .h file. Thanks for the help. I really appreciate it. |
|
#13
|
|||
|
|||
|
Big thanks, Scorpions4Ever. I hate to tell people not to do something, but not being able to explain why.
![]() |
|
#14
|
|||
|
|||
|
library
M.Hirsch,
can you direct me to a place to learn more about this process: for building a library, you would: - define the interface in a .h file - implement it in a .cpp file - compile it to a .o file - reuse the .o file, importing it into other projects using the .h file. in *nix? in windows? Thanks! J |
|
#15
|
||||
|