Iterating through a Vector of Pointers

Hey guys, If I have a Vector of pointers, how can I iterate through that vector, to access the name function of each object in the vector? There seems to be a problem with my implementation, the output returns only random symbols.

Implementation of the name() function in Drug.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//name() is a function returning the name in the parent class
string Drug::name()
 {
    string out = "Drug: ";
    out += mName + " [ ";
    //The problem is with this loop
    for (int k = 0; k < mComponents.size(); k++)
    {
	out += mComponents[k]->name() + ", ";
    }
    
    out += " ]";
    return out; 
 }


Drug.h for reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef DRUG_H
#define	DRUG_H

#include "Chemical.h"
#include "Component.h"
#include <vector>
#include <iostream>

using namespace std;

class Drug : public Component
{
	public:
		Drug(string name);
		virtual ~Drug();
		void administer();
		string name();
		void add(Component *component);
		virtual void mix();
	private:
		vector<Component*> mComponents;
};

#endif 


Component.h for reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef COMPONENT_H
#define COMPONENT_H

#include <string>
#include <iostream>

using namespace std;

class Component
{
	public:
		Component(string name);
		virtual void add(Component *component);
		virtual string name() = 0;

	protected:

		string mName;
};

#endif 
How do you use the Drug?
There are concrete classes defining different types of drugs, a drug named "Miracuru" for example will be created by:

Drug* d = new Miracuru();

However the point is, given the following vector: vector<Component*> mComponents;

How can I iterate through the vector of object pointers to access the name() function of each component object in the vector? The following code produces the incorrect results:

1
2
3
4
for (int k = 0; k < mComponents.size(); k++)
    {
	out += mComponents[k]->name() + ", ";
    }
How do you call add(Component *)?
Last edited on
For Example, in concrete class Miracuru, which is a drug to be created:

Miracuru.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef MIRACURU_H
#define	MIRACURU_H

#include "Drug.h"

class Miracuru: public Drug
{
	public:
		Miracuru();
		void mix();
};

#endif 


Miracuru.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Miracuru.h"

Miracuru::Miracuru()
   :Drug("Miracuru")
{
  
}

void Miracuru::mix()
{
    Component* c = new Chemical("trifluoperazine");
    Component* b = new Chemical("methylprednisolone");
    Component* d = new Chemical("acetaminophen");
    Drug* f = new Drug("Miracle Worker");
   
    add(c);
    add(b);
    add(d);
}
You're leaking memory on line 14 of Miracuru.cpp

Can you show the constructor Component::Component(std::string)?
Component.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Component.h"

Component::Component(string name)
{
    mName = name;
}

void Component::add(Component *component)
{
  
}

string Component::name()
{
    return mName;
}
So, Chemical is a Component, and therefore Chemical has name() ?

Did you:
1
2
3
Drug* d = new Miracuru();
d->mix():
std::cout << d->name() << '\n'

Not even "Miracuru"?

PS. "Miracle Worker" does not seem to be involved in anything.
I found the solution I was looking for, here it is:

1
2
3
4
5
vector<Component*>:: iterator it;
    for (it = mComponents.begin(); it !=mComponents.end(); ++it)
    {
	out += (*it)->name() + ", ";
    }


Thanks for the help guys.
That's the same thing as what you said did not work.
I think I fixed another small error in elsewhere in my code, where I forgot to return a needed value, anyway, I learned a lot about Vectors from this :)

I apologize for the misunderstanding, but thanks for trying to help!
Last edited on
Topic archived. No new replies allowed.