Pointer to a vector element

Hey, guys. I've been having a problem, I can't seem to use a pointer to a vector element.
This is the code I got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class C;

 class B {
	C * memberC;
public:
	void setMemberC(C* paramC);
};

 class A {
public:
	vector<B > vecB;
	vector<C > vecC;
	bool importB();
};

The problem is in the definition of importB(). In it, an object of type B is created and added to vecB. At a certain point, it should use the B::setMemberC with a pointer to an element of vecC as the argument.
I've tried vecB.back().setMemberC(&(vecC[i])); and even through iterators
1
2
vector <C>::iterator it = vecC.begin();
vecB.back().setMemberC(&(*it));

but neither was successful. Any suggestions?

[EDIT]:Problem solved. Solution here: http://www.cplusplus.com/forum/beginner/120822/
Last edited on
closed account (zb0S216C)
Based on the code you gave, you're attempting to convert from the address of a "B" object (a pointer) to a "B" object (non-pointer) -- the difference, of course, is night and day. Try removing the address-of operator from the "(vecC[i])" expression and see if that resolves the problem.

Wazzak
Last edited on
What I'm trying to do is to "link" an object of type C (within vecC) to a object of type B (within vecB). Since I don't want to create a new object C for every object B created, I chose the pointer type (it should point to an element of vecC). Hence the C* memberC.
The only thing that B::setMemberC does is memberC=paramC.
So, that can't be the issue.
Furthermore, by removing the & operator, eclipse automatically shows an error, whereas with it the argument is accepted and the code is ran.
Last edited on
Topic archived. No new replies allowed.