Passing vector to friend class

Hello everyone.

I am new in C++ and start to work in a code. Basically I create a vector from Main that is populated in class A, but when I try to read that vector in class B it does not work. I suspect that is related to the way I am passing the vector to B, because I print from A the vector's content and it has elements. (Probably my code in class A could be better)

Here is the code:

Class A.h
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
#ifndef A_H_
#define A_H_


#include <iostream>
#include <vector>

using namespace std;

class A {

	 friend class B;

private:

	 int newValue;
	 int newW;
public:

	A (vector <A>& );
	A (int, int);
	virtual ~A();

	int getValue() const;
	int getW() const;

	void fillVector(vector<A>& );

};

#endif /* A_H_ */ 


Class A.cpp
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
45
46
47
48
49
50
51
52
#include "A.h"
#include <random>
#include <stdlib.h>
#include <time.h>
#include <vector>

A::A (int value, int w){
	newValue = value;
	newW = w;
}

A::A(vector<A>& bagA){
 	fillVector(bagA);
}


A::~A (){

}

int A::getValue() const{
 	return newValue;
}

int A::getW() const{
 	return newW;
}


void A::fillVector(vector<A>& bagA){
	int value;
	int w;

 	int mxC = 900;
	int mnC = 500;

 	int mxV = 100;
 	int mnV = 50;

	 int mxP = 20;
	 mnP = 1;

	 int size = 10; //

	for (int i = 0; i < size; i++){
		value = rand()% (mxV - mnV) + mnV;
 		w = rand()% (mxP - mnP) + mnP;
		A newA (value, w);

 		bagA.push_back(newA);
	}
}



Class B.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef B_H_
#define B_H_

#include "A.h"
#include <iostream>
#include <vector>

using namespace std;

class B {

public:

	B(vector<A>&);
	virtual ~B();
};

#endif /* B_H_ */


Class B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "B.h"

#include <iostream>
#include <vector>

using namespace std;

B::B(vector <A>& bagA) {
	 cout << bagA[0].getValue()<<endl; //This is only to see if Class B is accessing the vector from class A

}

B::~B() {

}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "A.h"
#include "B.h"

#include <vector>

using namespace std;

int main() {
        srand(time(0));
	vector<A> bagA;
	A (bagA);
	A (0, 0);
	B(bagA);

	return 0;
}


In this case, How could I pass the vector populated in A to B and read it ?
Last edited on
What do you mean by "It doesn't work"? What doesn't work? By this I mean, what is the compiler telling you (if this is a compiler issue) or what output are you getting when you call the constructor for `B`? Try to be intentionally elaborate when describing a coding problem
The errors are marked in the main:

 
vector<A> bagA;


Error
1
2
Description
'bagA' has a previous declaration as 'std::vector<A> bagA'


 
A (bagA);


Error
1
2
Description
conflicting declaration 'A bagA'


 
B (bagA);


Error
1
2
Description
conflicting declaration 'B bagA'


When I print the vector has the content that was generated randomly. I have tried different solutions posted in this forum, but always generate a error in the Main or in class B.
What do you think lines 11-13 of main.cpp do?

If you're going to instantiate A and B there, you need to name the instances.

Line 12: A has a constructor that takes two ints, not three.

a.cpp line 34: You should only ever call srand() once in your program. The best place to call srand() is early in main. Calling srand multiple times in the same second causes the random number generator to be reset back to the beginning resulting in the same sequence of random numbers.

How could I pass the vector populated in A to B and read it ?

A has no knowledge of the vector. Only A's second constructor and fillVector have knowledge of the vector because it's passed as an argument. If you want instances of A to know about the vector, A will need to make a copy of the vector (preferred), or store a pointer to the vector.


Last edited on
In lines 11-13 I was calling the constructors from A (according to me), but I am calling just the constructor that is receiving the two ints (the third int is a mistake) and works properly.

I put srand() in the Main.

And for the class B I made the following change:

Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
int main() {

	srand(time(0));

	vector<A> bagA;
	
	A (0, 0);

	B (&bagA[0]);

	return 0;
}


Class B.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef B_H_
#define B_H_

#include "A.h"
#include <iostream>
#include <vector>

using namespace std;

class B {

public:

	B(vector<A>*);
	virtual ~B();
};

#endif /* B_H_ */ 


Class B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "B.h"

#include <iostream>
#include <vector>

using namespace std;

B::B(vector <A>* bagA) {
	 cout << bagA[0].getValue()<<endl; //This is only to see if Class B is accessing the vector from class A

}

B::~B() {

}


But now the error says in class B:

Description
'class std::vector<A>' has no member named 'getValor'

I do not want to make a copy of the vector, because it has to be larger, that size is just an example, and I want to use functions from A in B to do not rewrite getter functions in B (because this class is only to read the vector)
Last edited on
Lines 7-9 are still bogus. As I said before, you must name the instances of the objects.

7
8
9
	A  obja (0, 0);

	B objb (&bagA[0]);


'class std::vector<A>' has no member named 'getValor'

Is that a typo? I see nothing in the code you say you changed called "getValor()".
Did you mean getValue().

I want to use functions from A in B to do not rewrite getter functions in B (because this class is only to read the vector

As I pointed out before, neither A nor B know anything about the vector, except where it's been passed as an argument. i.e. there is NO vector in either A or B.



Thank you AbstractionAnon, after thinking what you were saying, it worked.

And yes, that was a typo, my original code is in another language, so I changed some words for everyone to understand.

My solution was to declare the vector in Class A and create a function returning the vector. Then in Class B I declared another vector, there I call the function getVector from Class A.
Topic archived. No new replies allowed.