以下问题:
我有两个类来组织我的配置文件和我的数据库的加载过程。 我通过初始化它们并调用它们各自的函数来使用这些类。 我现在正试图在我的代码中加入一些结构,所以我想让我的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();
}
};
但是现在我不能在我的其他函数中调用这两个类的函数,因为&#34; db&#34;和&#34; cfg&#34;还没有定义。
我是新手编程的复杂性所以我想问一下解决这个问题的最佳方法是什么?
我希望能够从我的主类中的所有函数调用我的ConfigFile.cpp和VN_DB.cpp中实现的所有函数。
答案 0 :(得分:1)
getConfig
和getDatabase
似乎都是返回某些内容的函数的名称。你的没有。您应该返回您在里面初始化的内容(如果可能的话,按值计算),那么您将能够使用它:
map<string, string> getConfig()
{
...
return config;
}
map<string, vector<string>> getDatabase()
{
...
return data;
}