Probleme mit string und vector<string>
-
Hallo Leute!
ich habe ein problem mit meiner exception klasse und zwar kommt dann bei der ausgabe nur müll raus
die klasse Exception und Debug:// Debug.h class Debug { private: Debug(); void addData(const string & data); const char* getList(); private: vector<string> m_data; }; //Debug.cpp void Debug::addData(const string & data) { m_data.push_back(data); } const char* Debug::getList() { string tmp; for (int i = 0; i < m_data.size(); i++) tmp+=m_data[i]; return m_data.c_str(); } //Exception.h class Exception { public: Exception(Debug * debug); const char * what() const; ~Exception(); private: Debug * m_debug; }; // Exception.cpp Exception::Exception(Debug * debug) : m_debut(debug) { } const char* Exception::what() const { return m_debug->getList(); }
der aufruf:
void test() { int i = 0; if (i==0) throw Exception(db); } int main() { Debug * db = new Debug(); db->addData("Hallo"); ... try { test(); } catch( Exception e ) { cout << e.what(); } ... return 0; }
was ist falsch?
danke
-
es ist im obrigen beispiel ein fehler, undzwar ist Debug *db eine globale variable
-
- Exception ist eine Klasse aus der STL
- Wenn du eine spezielle Exception brauchst, dann erbe von Exception, dann kannst dir das komische rumgepointere evtl sparen
- Was ist genau das problem?
[quote=Galilo]ich habe ein problem mit meiner exception klasse und zwar kommt dann bei der ausgabe nur müll raus [/quote]
ist nicht sehr aufschlussreich
-
ok, ich habe es verändert.
ausgabe ist aber immer noch:
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠↑¶D╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠È ╠╠╠╠╠╠╠╠
-
Galilo schrieb:
const char* Debug::getList() { string tmp; for (int i = 0; i < m_data.size(); i++) tmp+=m_data[i]; return m_data.c_str(); }
Ah, der Klassiker: Rückgabe eines Zeigers auf ein lokales Objekt.
Gib zum Beispiel std::string zurück und Du bis das Problem los.MfG Jester
-
danke!
problem gelöst