Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    8
    Rep Power
    0

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    :confused:
    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...
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    8
    Rep Power
    0
    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)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    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.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    8
    Rep Power
    0
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    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 :rolleyes:
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    8
    Rep Power
    0
    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.
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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?
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    8
    Rep Power
    0
    For one dimentional arrays, yes, that works. For two dimentional arrays, it doesn't.
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    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 06:26 PM.
  20. #11
  21. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    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 06:27 PM.
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    8
    Rep Power
    0
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    Big thanks, Scorpions4Ever. I hate to tell people not to do something, but not being able to explain why. :)
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Right Coast
    Posts
    25
    Rep Power
    0

    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
  28. #15
  29. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    ahuimanu, you've described the process correctly. It applies to pretty much any OS/C compiler. Only the names of the tools used are different. See my explanation here Linakge of Standard Library Functions
    about how libraries are built. Hope this helps!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo