离开作用域后,类变量会自动删除,我该如何防止这种情况发生?

问题描述 投票:0回答:1

我从另一个类(B类)函数调用类(A类)函数时遇到问题,其中函数在函数之后删除了我试图调用的类(A类)中构造的变量完成并返回到我调用的函数(C类)。发生了什么,我该如何解决这个问题?

我已经尝试使用指向该类的指针并动态分配整个类,但变量仍然被删除。


#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;
            }
        }
    }

我期望用随机生成的数字构建向量B和C,从vectorA中选择变量。但是我得到的是当它生成出兰德()时,它会在Project1.exe中显示0x00F1C77F处的未处理异常错误:0xC0000094:整数除零。因为vectorA的大小为0,因为它在程序离开该类的范围后被删除。

c++ class object dynamic-memory-allocation calling-convention
1个回答
1
投票

只要您的类对象执行,类成员就会活动。问题不在于对象的生命周期,而是因为B::aobject从来没有调用buildvectorA,所以它的vectorA是空的,所以vectorA.size() == 0。然后你试着计算rand() % vectorA.size(),并且0的(整数)除法是一个非常糟糕的主意。

您可能想为qazxsw poi创建一个构造函数,并在那里调用qazxsw poi以确保它始终正确:

A

请注意,generaterandnum现在是私有方法。我们不希望外面的任何人打电话给它,因为他们不关心如何生成随机数。他们只想得到那些随机数。

这也意味着你的类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; } }; 不需要调用buildvectorA,事实上,它甚至不需要任何对象C

© www.soinside.com 2019 - 2024. All rights reserved.