调用已在另一个函数中初始化的类

Calling a class that has been initialized in another function

本文关键字:初始化 函数 另一个 调用      更新时间:2023-10-16

以下问题:

我有两个类来组织配置文件和数据库的加载过程。我通过初始化它们并调用它们各自的函数来使用这些类。我现在正试图在代码中加入一些结构,所以我想使我的主类基于函数。以下是我的主类(Wholething.cpp)的代码示例

#include "stdafx.h"
#include "VN_DB.cpp"
#include "iostream"
#include "sstream"
#include "map"
#include "string"
#include "iterator"
#include "VN_chk.cpp"
#include "Execute.cpp"
#include "ConfigFile.cpp"
using namespace std;
class theWholeThing {
int db_width;
int act_free;
string act_free_str;
map <string, string> config;
string master_path;
string download_path;
int ausfuehren;
map <string, vector<string>> data;
private:
void getConfig() {
    //Lade CONFIG via ConfigFile
    ConfigFile cfg("config.cfg");
    config = cfg.getCFG();
    string act_free_str = config["act_free"];
    act_free = atoi(act_free_str.c_str());
    string db_width_str = config["db_width"];
    db_width = atoi(db_width_str.c_str());
    master_path = config["master_path"];
    download_path = config["download_path"];
}

void getDatabase() {
    //Lade Database via VN_DB
    VN_DB db ("export.csv", db_width, act_free);
    data = db.getData();
    for (map <string, vector <string>>::iterator it = data.begin(); it != data.end(); it++) {
        string id;
        string buffer;
        id = it->first + " ";
        for (unsigned int i = 0; i < it->second.size(); i++) {
            buffer = buffer + it->second[i];
        }
        cout << buffer << endl;
    }
}
void ioMenu() {
    cout << "@@@@@@@@@@@@@@@@@@@@    " << "Menu" << "    @@@@@@@@@@@@@@@@@@@@" << endl;
    cout << "Welche Funktion moechten sie nutzen? n" << endl;
    cout << "(1) Neues Programm n" << "(2) Programm löschen n" << "(3) Programm suchen n" << "(4) Programm updaten n" << "(5) Alle Programme updaten n" << "(6) Datenbank exportieren n" << endl << endl;
    cin >> ausfuehren;
    switch (ausfuehren) {
    case 1: //...
    case 2: //...
    case 3: //...
    case 4: //...
    case 5: //...
    case 6: //...
    default: 
    }
}
void addProgram() {
    string temp_name;
    string temp_path;
    vector <string> ibuffer;
    cout << "Name des Programms: " << endl;
    cin >> ibuffer.at(0);
    cout << "Pfad der Installationspakete" << endl;
    cin >> ibuffer.at(1);
    cout << "Name des Installationspaketes" << endl;
    cin >> ibuffer.at(2);
    cout << "Versionsnummer des Installationspaketes" << endl;
    cin >> ibuffer.at(3);
    db.newProg("03", ibuffer);
    act_free++;
    stringstream ss;
    ss << act_free;
    act_free_str = ss.str();
    config["act_free"] = act_free_str;
    cfg.WriteConfig(config);
}

public:
theWholeThing() {
    getConfig();
    getDatabase();
    ioMenu();
}
};

但是现在我不能在其他函数中调用这两个类的函数,因为"db"answers"cfg"还没有定义。

我是如此复杂的编程新手,所以我想问解决这个问题的最佳方法是什么?

我希望能够从主类中的所有函数调用ConfigFile.cpp和VN_DB.cpp中实现的所有函数。

getConfiggetDatabase似乎都是返回某些内容的函数的名称。你的没有。您应该返回内部初始化的内容(如果可能的话,通过值),然后您就可以使用它了:

map<string, string> getConfig()
{
    ...
    return config;
}
map<string, vector<string>> getDatabase()
{
    ...
    return data;
}