dringende Hilfe bei Libaray und return gebraucht
-
hier erstmal meine Code:
#include <stdlib.h> #include <stdio.h> #include <string> #include<iostream> #include<fcntl.h> #include<sys/stat.h> #include<fstream> #include<vector> using namespace std; class kontaktlist { public: ifstream is; ofstream off; kontaktlist(char* begin,char *neu) { readlist(begin); writelist(begin,neu); clearlist(begin,neu); } void readlist (char *begin){ string end=".c"; string all = begin + end; char *next_r = &all.at(0); is.open(next_r,ios::in); if (is.is_open()) { is.seekg (0, ios::end); int length_r = 0; length_r = is.tellg(); is.seekg (0, ios::beg); char *buffer_r = new char[length_r]; char foo_r[length_r]; is.read (buffer_r,length_r); is.close(); memcpy(foo_r,buffer_r,length_r); return (foo_r); }else{ is.close(); off.open(next_r,ios::out|ios::app); off<<""; off.close(); return NULL; } } void writelist (char *begin,string neu){ string end=".c"; string all = begin + end; char *next_w = &all.at(0); is.open(next_w,ios::in); if (is.is_open()) { is.close(); off.open(next_w,ios::out|ios::app); neu= neu + ';'; char *neu_w = &neu.at(0); off<<neu_w; off.close(); }else{ is.close(); off.open(next_w,ios::out|ios::app); off<<""; off.close(); } } void clearlist(char *begin_c,char *name){ int i=0; int anz=0; string end_c =".c"; string all_c = begin_c + end_c; char *next_c = &all_c.at(0); is.open(next_c,ios::in); if(is.is_open()){ is.seekg (0, ios::end); int length_c = 0; length_c = is.tellg(); char vor[length_c]; is.seekg (0, ios::beg); //Abfangschleife:Datei ist leer if(length_c==0) { is.close(); }else{ char *buffer_c = new char[length_c]; is.read (buffer_c,length_c); memcpy(vor,buffer_c,length_c); for(int k= 0;k<length_c;k++) { if(vor[k]==';') { anz++; } } string foo_c[anz]; for(int h= 0;h<length_c-1;h++) { if(vor[h]==';') { i++; }else{ foo_c[i] +=vor[h]; } } bool kontr= false; for(int j=0;j<i;j++) { if(foo_c[j]== name) { for (int l=j; l<i;l++) { foo_c[l]=foo_c[l+1]; } kontr=true; } } //Kontrallschleife: eigegebener Benutzer existiert nicht in der Kontaktliste if(kontr== false) { is.close(); }else{ is.close(); off.open(next_c,ios::out); for(int l= 0;l<i-1;l++) { off<<foo_c[l]; off<<';'; } off.close(); } } }else{ //Datei existiert nicht und wirde deshalb erzeugt is.close(); off.open(next_c,ios::out|ios::app); off<<""; off.close(); } } }; int main(){ kontaktlist test("cat","hallo"); return 0; }Nun in readlist wird eine Datei geöffnet.
Nun soll der Inhalt der Datei in einem Array returnt werden.
Wenn die Datei leer ist soll ein leeares Array returnt werden (mgl. dynamisch also vector)
beim Kompilken gibt gcc mir folgendes zurück:Dante@linux:~/Programme/CPP> g++ -o realclearkontaktlist realclearkontaktlist.cpp
realclearkontaktlist.cpp: In member function ‘void kontaktlist::readlist(char*)’:
realclearkontaktlist.cpp:41: error: return-statement with a value, in function returning 'void'
realclearkontaktlist.cpp:47: error: return-statement with a value, in function returning 'void'
Dante@linux:~/Programme/CPP>1. Woran liegt das und wie kann man das Problem beheben??
2. Wie wandelt man das Programm in eine Libary um? (bitte ausführlich, habe sowas noch nie gemacht, geschweige denn die Libaraies verstanden)pls helped me.
-
Wenn du einen Wert zurückgeben willst, darfst du die Funktion nicht "void" deklarieren, sondern mit dem gewünschten Rückgabetyp:
vector<string> readlist(const string& name);(btw, Pointer auf lokale Variablen (foo_r) solltest du sowieso nicht zurückgeben und den richtigen Umgang mit string's mußt du auch noch üben ;))
-
Ok danke ich schreibs um, aberwie mache ich daraus ne Bibliothek??