Alexander Helmer
Alexander Helmer

Reputation: 1

Calling a class that has been initialized in another function

following problem:

I have two classes that organize the loading procedure of my config file and my database. I use those classes by initializing them and calling their individual functions. I am now trying to get some structure into my code, so I want to make my main-class function-based. Here is a code sample of my main-class (theWholeThing.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();
}

};

But now I cant call the functions of these two classes in my other functions because "db" and "cfg" are not defined yet.

I am new to programming in such complexity so I want to ask what would be the best way to fix this?

I want to be able to call all functions implemented in my ConfigFile.cpp and VN_DB.cpp from all of my functions in my main class.

Upvotes: 0

Views: 72

Answers (1)

LogicStuff
LogicStuff

Reputation: 19607

Both getConfig and getDatabase seem like names for functions that return something. Yours don't. You should return what you initialized inside (by value, if that's possible), then you'll be able to work with it:

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

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

Upvotes: 1

Related Questions