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)
#include "B.h"
#include <iostream>
#include <vector>
usingnamespace 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>
usingnamespace 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 ?
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
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.
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.
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;
}
#include "B.h"
#include <iostream>
#include <vector>
usingnamespace 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)
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.