调用已在另一个函数中初始化的类
Calling a class that has been initialized in another function
以下问题:
我有两个类来组织配置文件和数据库的加载过程。我通过初始化它们并调用它们各自的函数来使用这些类。我现在正试图在代码中加入一些结构,所以我想使我的主类基于函数。以下是我的主类(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中实现的所有函数。
getConfig
和getDatabase
似乎都是返回某些内容的函数的名称。你的没有。您应该返回内部初始化的内容(如果可能的话,通过值),然后您就可以使用它了:
map<string, string> getConfig()
{
...
return config;
}
map<string, vector<string>> getDatabase()
{
...
return data;
}
相关文章:
- 正在初始化函数指针的constexpr数组
- 我应该在C++中使用构造函数还是初始化函数
- 使用 extern const 初始化函数
- 初始化函数中的静态变量,而不是在文件开始时
- 如何初始化函数中指针数组的大小
- 正在初始化初始化函数中的静态类成员
- call_once初始化函数
- SWIG + CMAKE:缺少初始化函数
- 如何创建在 c++ 中只读取一次的初始化函数
- 类内的强制初始化函数
- 使用元编程在编译时初始化函数数组
- 数组初始化函数:将数组作为指针传递:C++
- 从nodejs模块初始化函数报告失败
- 导入错误:动态模块未定义初始化函数
- 大括号初始化函数指针数组:从 'void (*(*)())()' 到 'void (*)()' 的转换无效
- 奇怪的行为,用 int 初始化函数指针
- 使用 init_seg 调用初始化函数
- 正在初始化函数内部的std::数组
- 初始化函数内部的指针.它是如何工作的
- JNI初始化函数以及缓存类和字段