Manager Arrays

I'm having a problem accessing member functions through a manager array. In this example, I have 3 different types of guns that inherit from a base class. I'm getting a problem when the output function tries to access my virtual output function.
1
2
3
4
5
6
7
8
9
10
11
12
...

int main()
{
	manager myInventory;

	myInventory.addGun(new pistol(8, 0, 1, 12, 100, "Colt", ".45", "FMJ", "closed"));

	myInventory.output();

        ...

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

class manager
{
        ...

        bool addGun(gun* g)
	{
		if(counter > 10) {return false;}
		
		Array[counter++] = g;
		return true;
	}

        ...
	void output()
	{
		for(int i = 1; i <= 10; i++)
			Array[i] -> output();
	}

"pistol.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        ...
        void output(std::ostream& s) const
	{
	s <<"The safety is in position" << safetyOn<< ". The slide position is" 
<< slidePos  << ". The target is at " << TarCond << " Health. \nThere are " << 
roundsLeft  << ".\n ";
	}

	friend std::ostream& operator<<(std::ostream& s, const pistol& p)
	{
		p.output(s);
	}

        ...


Any help would be greatly appreciated! I'm n ot sure what I'm doing wrong here!
Last edited on
Array indices go from 0 to size-1 where size is the number of elements in the array. Therefore, if an array has size 10 a for loop that visits all elements would look like:

1
2
for ( unsigned i=0; i<10; ++i )
     visit(Array[i]) ; 

and not
1
2
for ( unsigned i=1; i<=10; ++i )
     visit(Array[i]) ; 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        bool addGun(gun* g)
	{
		// if(counter > 10) {return false;}
                if( counter >= ARRAY_SIZE ) return false ;
		
		Array[counter++] = g;
		return true;
	}

        // ...

	void output()
	{
		// for(int i = 1; i <= 10; i++)
                for(int i = 1; i < counter ; ++i )
			Array[i] -> output();
	}
THanks! got the memory errors figured out, but my program still isn't outputting anything to the console.
The gun type "pistol" takes an argument in it's output function. Why isn't one supplied to it in manager::output? Is it a virtual function in the gun type base class definition? Do the signatures match in the derived classes?
yes my base class has a virtual function
 
virtual void output(){}

yes my base class has a virtual function


... that does nothing. And your derived class doesn't override it (the signatures are not the same -- that's overloading, not overriding) so you get the base class behavior.
Gotcha. How would I pass one of my new objects into a different function by reference?

gun.h
 
virtual void reload(gun&) {}


pistol.h
1
2
3
4
5
6
7
8
9
10
11
12
void reload(pistol& p) 
		{
			SafetyOn(p);
			if(slidePos == "open")
					roundsLeft = MagCap; 	
			else
			{
				slidePos = "open";
				roundsLeft = MagCap + 1;
			}
			slidePos = "closed";
		}


Array.h
1
2
3
4
5
void reload(gun& g)
	{
		for(int i = 0; i < counter; i++)
			Array[i] ->  reload(g);
	}


source.cpp
1
2
3
4
5
manager myInventory;

	myInventory.addGun(new pistol(8, 0, 1, 12, 100, "Colt", ".45", "FMJ", "closed"));

myInventory.reload(???);
Class methods have a pointer to the invoking object passed implicitly in the form of the this pointer. There is no reason for the reload method to take an explicit argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

struct Gun
{
    virtual void reload() { std::cout << "Gun::reload\n" ; }
};

struct Pistol : public Gun
{
    static const unsigned MagCap = 17 ;

    std::string slidePos ;
    unsigned roundsLeft ;
    
    Pistol( ) : slidePos("closed"), roundsLeft(MagCap) {}


    void reload()
    {
        std::cout << "Pistol::reload\n" ;

        if ( slidePos == "open" )
            roundsLeft = MagCap ;
        else
        {
            slidePos = "open" ;
            roundsLeft = MagCap+1 ;
        }
        slidePos = "closed" ;
    }
};

int main()
{
    Pistol p ;
    Gun g ;

    Gun* ptr = &p ;
    ptr->reload() ;

    ptr = &g ;
    ptr->reload() ;
}


http://ideone.com/LEBCAT
Last edited on
nevermind turns out I didnt need to pass them through to the function
I ended up being able to not use any arguments to get everything done. Thanks for all the help. but just so I know, is it possible to pass new objects into functions?
Topic archived. No new replies allowed.