Reputation: 11
I'm having an issue when calling a classes (class A) function from another classes (class B) function where the variables from the class i am trying to call (class A) which are constructed in that class are deleted after the function is completed and returned to the function from which I am calling from (class C). What is happening and how do I fix this?
Ive tried using pointers to that class and dynamically allocating the entire class but the variables are still deleted.
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
class A {
private:
vector<string> vectorA;
string stringA[5] = { "1", "2", "3", "4", "5" };
string stringB[5] = { "6", "7", "8", "9" };
public:
string generaterandnum() {
int num1 = NULL;
num1 = rand() % vectorA.size();
string card = vectorA[num1];
vectorA.erase(vectorA.begin() + num1);
return card;
}
void buildvectorA()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
vectorA.push_back(stringA[j] + stringB[i]);
}
}
return;
}
};
class B {
private:
vector<string> vectorB;
vector<string> vectorC;
A aobject;
public:
void buildvectorBandC() {
for (int i = 0; i < 5; i++) {
vectorB.push_back(aobject.generaterandnum());
}
for (int i = 0; i < 5; i++) {
vectorC.push_back(aobject.generaterandnum());
}
}
void displayvector() {
cout << "Vector: ";
for (size_t i = 0; i < vectorB.size(); i++) {
cout << "[" << vectorB[i] << "] (" << i << ") ";
}
cout << endl;
}
};
class C {
friend int main();
void programrun(int option) {
A* a = new A;
a->buildvectorA();
B* b = new B;
if (option == 0) {
cout << "Here is the vector that just has been constructed" << endl;
b->buildvectorBandC();
while (true) {
b->displayvector();
}
}
}
};
int main() {
srand((unsigned int)time(NULL));
cout << "Hello" << endl;
cout << "Enter (R) to run the program." << endl;
char input;
while (true) {
cin >> input;
input = toupper(input);
if (input == 'R') {
C cobject;
cobject.programrun(0);
return false;
}
else {
cout << "Invalid input" << endl;
}
}
}
I expected to build vector B and C with randomly generated numbers that pick the variables from vectorA. But what I got was when it gets to generaterandnum() it displays an error saying Unhandled exception at 0x00F1C77F in Project1.exe: 0xC0000094: Integer division by zero. because vectorA has a size of 0 because it was deleted after the program left the scope of that class.
Upvotes: 0
Views: 138
Reputation: 22219
Class members live as long as your class objects do. The problem is not with object lifetime, but with the fact that B::aobject
is has never had buildvectorA
called, so it's vectorA
is empty, so vectorA.size() == 0
. You then try to calculate rand() % vectorA.size()
, and (integer) division by 0
is a pretty bad idea.
You may want to create a constuctor for A
and call the generaterandnum
there to make sure it's always proper:
class A {
private:
vector<string> vectorA;
string stringA[5] = { "1", "2", "3", "4", "5" };
string stringB[5] = { "6", "7", "8", "9" };
void buildvectorA()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
vectorA.push_back(stringA[j] + stringB[i]);
}
}
}
public:
A() {
buildvectorA();
}
string generaterandnum() {
int num1 = NULL;
num1 = rand() % vectorA.size();
string card = vectorA[num1];
vectorA.erase(vectorA.begin() + num1);
return card;
}
};
Note that buildvectorA
is now private method. We don't want anyone from outside to call it, because they don't care how are the random numbers generated. They just want to get those random numbers.
This also means that your class C
doesn't need to call buildvectorA
, and in fact, it doesn't even need to have any object A
.
Upvotes: 1