trying to pass a array of a class

This is a discussion on trying to pass a array of a class within the C++ Programming forums, part of the General Programming Boards category; well i have one solution to my code right now but i would prefer to keep the main function simplier ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    trying to pass a array of a class

    well i have one solution to my code right now but i would prefer to keep the main function simplier

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class monster
    {
    public:
    	int m_hps;
    };
    
    void displayMonsterHealth(monster &mon);
    
    int main()
    {
    	monster mon[4];
    	mon[0].m_hps = 23;
    	mon[1].m_hps = 12;
    	mon[2].m_hps = 31;
    	mon[3].m_hps = 1;
    
    
    	for( int i = 0; i < 4; ++i)
    		displayMonsterHealth(mon[i]);		// this works to display the information from class
    											// monster but i would prefer......
    
    
    
    
    	system("pause");
    	return 0;
    }
    
    void displayMonsterHealth(monster &mon)
    {
    	cout << mon.m_hps << endl;				// to be able manage the same thing with the for(int i = 0; i < 4; ++i)
    }									// here and leave the Main function simple. Anyone know how i can pass the 
    									// whole array of mon into this function then use "for" to check them.
    i seen a few other posts, i think similar and one said something about vectors and iters, would like a opinion on that please

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Code:
    #include <iostream>
    #include <vector>
    
    std::vector<monster> monsters;
    
    void AddMonster(int hps) {
    	monster m;
    	m.m_hps = hps;
    
    	monsters.push_back(m);
    }
    
    void DisplayAll() {
    	std::vector<monster>::iterator itr;
    
    	for (itr = monsters.begin(); itr != monsters.end(); ++itr )
    		std::cout "This monster's health is: " << itr.m_hps << std::endl;
    }
    int main() {
    	AddMonster(10);
    	AddMonster(43);
    	AddMonster(25);
    	AddMonster(12);
    
    	DisplayAll();
    
    	return 0;
    }
    Last edited by nicoqwertyu; 03-14-2010 at 11:26 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Code:
    void displayAllMonsterHealth(monster *mon, size_t size){
      for( int i = 0; i < size; ++i)
        cout << mon[i].m_hps << endl;
    }
    Pass your mon variable for mon, and 4 for size.

    And don't listen to nicoqwertyu. No need for global variables here.
    Last edited by King Mir; 03-15-2010 at 02:40 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Aye, no need for a global. You could refactor DisplayAll() to DisplayAll(const std::vector<monster>& monsters) instead.
    I agree with the use of vectors over pointers though.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    And I agree with vectors over passing pointer + size.
    A vector will basically be your array. Then all your functions can take the vector by reference and it'll all work out a you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    7
    thanks for your help, i will go and learn to pass my array into my function as a vector, i will look for a tutorial on this but if you have any suggestions of where some "good" tutorials on this are that would be great too.

    thanks again

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by morngrym View Post
    thanks for your help, i will go and learn to pass my array into my function as a vector, i will look for a tutorial on this but if you have any suggestions of where some "good" tutorials on this are that would be great too.

    thanks again
    Online tutorial are insufficient for learning C++. You really need a book.

    The suggestion is not to pass your array as a vector, but to use a vector in place of your array. Nicoqwertyu's code does correctly show how vectors are used. _Mike explains how to declare a function that takes a vector as a reference, so you do not have to use a global.

    If you want to use an array, use my code. Note that I fixed an error in it. Principally it's more versatile, since you can pass a vector as a pointer, but not an array as a vector. But vectors are more important to learn than arrays.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 03:20 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Replies: 1
    Last Post: 10-21-2007, 08:44 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21