problem mit g++
-
Hallo
bin newbie was sachen c++ in linux angeht,
Ich habe mir mal versucht eine klasse zum datei zugriff zu schreiben
die sieht so aus:////////////////////////////////// //base headers needed: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> //needed for example #include <iostream> ///////////////////////////////// //constants... (flags,...) #define READONLY 0; //read only #define WRITEONLY 1; //write only #define READWRITE 2; //read and write ///////////////////////////////// //class definittion class mkFile { protected: const char* str_FileName; int n_Mode; int n_FileId; public: mkFile(); ~mkFile(); int openFile(const char* strFileName,int nOpenFlag); bool closeFile( void ); bool IsReadOnly( void ); bool IsWriteOnly( void ); bool IsReadWrite( void ); }; mkFile::mkFile() { n_FileId = 0; } mkFile::~mkFile() { } int mkFile::openFile(const char* strFileName,int nOpenFlag) { if(n_FileId == 0) { str_FileName = strFileName; n_Mode = nOpenFlag; n_FileId = open(strFileName,nOpenFlag); //returns a file id. return n_FileId; }else{ return 0; } } bool mkFile::closeFile( void ) { if(n_FileId < 0) { bool bClosed = (close(n_FileId) > 0); n_FileId = 0; return bClosed; } else { return false; } } //dies ist nicht ganz richitg von der funtion her weiss ich... bool mkFile::IsReadOnly( void ) { return (bool)(n_Mode & READONLY == READONLY); } bool mkFile::IsWriteOnly( void ) { return (bool) (n_Mode & WRITEONLY == WRITEONLY); } bool mkFile::IsReadWrite( void ) { return (bool) (n_Mode & READWRITE == READWRITE); } ////////////////////////////////// //test program int main( void ) { mkFile* pFile = new mkFile; if( pFile->openFile("/home/valen16/a.txt",READONLY) ) { cout << "File opned..." << endl; } else { cout << "Couldn't open the file..." << endl; } if( pFile->IsReadOnly()) { cout << endl << "File is read only" << endl; } else { cout << endl << "Hmm it isn't..." << endl; } pFile->closeFile(); delete pFile; }
nun bekomme ich aber diesen fehler code
wann ich es mit : g++ file.cc -o file -Wall
kompiliere:
file.cc: In member functionbool mkFile::IsReadOnly()': file.cc:82: parse error before
;' token
file.cc: In member functionbool mkFile::IsWriteOnly()': file.cc:87: parse error before
;' token
file.cc: In member functionbool mkFile::IsReadWrite()': file.cc:93: parse error before
;' token
file.cc: In functionint main()': file.cc:104: parse error before
;' token
file.cc: At global scope:
file.cc:120: syntax error before `->' tokenwas ist mein probelm???
valen16
[url] www.binary-pulse.org [/url]
-
warum postest du uns den ganzen code? das kann man doch noch um 95 % kürzen.
-
return (bool)(n_Mode & READONLY == READONLY);
- der cast ist überflüssig, macht er automatisch wg. rückgabetyp der fkt.
- ich steig nicht ganz durch, was das '&' soll. tippfehler? soll es "&&" heissen? wenn ja: du hast den fehler 3x a lá copy'n'paste gemacht
besser das mal aus, und schau obs dann geht.ach ja: nicht immer solche mengen code posten, nur das woraufs ankommt. das schreckt sonst so ab (wer liest sich freiwillig 2 din a 4 seiten code durch...)
-
Beim Überfliegen:
Original erstellt von <valen16>:
**```cpp
#define READONLY 0; //read only
#define WRITEONLY 1; //write only
#define READWRITE 2; //read and writeWeg mit den Semikola.
open schlägt gerne mal fehl, char* will man nicht per = 'kopieren', deine Bitoperationen sind auch irgendwie seltsam -- ein 'only' hätte ich nämlich mit 'nur' übersetzt und dann reicht ein biliger Vergleich mit == ...
-
oder benutze einfach richtige konstanten
const int READONLY = 0;
-
Original erstellt von <valen16>:
///////////////////////////////// //constants... (flags,...) #define READONLY 0; //read only #define WRITEONLY 1; //write only #define READWRITE 2; //read and write
Kann man die nicht als "static const int" innerhalb deiner Klasse ablegen?
Alternativ pack' mal einen eigenen Namespace um das ganze rum damit das nicht einfach so die Umwelt verschmutzt.
Original erstellt von <valen16>:
///////////////////////////////// //class definittion class mkFile { protected: const char* str_FileName; int n_Mode; int n_FileId; public:
Warum ein "int n_FileId"?
Verwende statt open() die Funktion fopen() und deklariere n_FileId als FILE*, dann brauchst du auch die drei zusäztlichen C Header nicht mehr und du kommst mit <cstdio> und <iostream> aus.Original erstellt von <valen16>:
cout << "File opned..." << endl;
Wo machst du denn einen
namespace std;
? Oder wolltest du
std::cout << "File opened..." << std::endl;
schreiben.
-
So in etwa stelle ich mir das vor:
//////////////////////////////////////////////////////////////// // base headers needed: #include <cstdio> // needed for example #include <iostream> namespace VALEN { enum mkFileAttr { READONLY, WRITEONLY, READWRITE }; ///////////////////////////////// // class definittion class mkFile { protected: const char* str_FileName; enum mkFileAttr n_Mode; FILE* n_FileId; public: //////////////////////////////////////////////////////////////// // Konstruktor / Destruktor mkFile() : n_FileId(0) {}; ~mkFile() {}; //////////////////////////////////////////////////////////////// bool openFile(const char* strFileName, mkFileAttr nOpenFlag) { if(n_FileId == 0) { str_FileName = strFileName; n_Mode = nOpenFlag; switch(nOpenFlag) { case READONLY: n_FileId = fopen(strFileName, "r"); break; case WRITEONLY: n_FileId = fopen(strFileName, "w"); break; case READWRITE: n_FileId = fopen(strFileName, "rw"); break; default: n_FileId = fopen(strFileName, "r"); break; } return true; } else { return false; } }; //////////////////////////////////////////////////////////////// bool closeFile( void ) { if(n_FileId < 0) { bool bClosed = (fclose(n_FileId) > 0); n_FileId = 0; return bClosed; } else { return false; } }; //////////////////////////////////////////////////////////////// bool IsReadOnly( void ) { return (n_Mode == READONLY) ? true : false; }; bool IsWrieOnly( void ) { return (n_Mode == WRITEONLY) ? true : false; }; bool IsReadWrite( void ) { return (n_Mode == READWRITE) ? true : false; }; }; } /* namespace VALEN */ //////////////////////////////////////////////////////////////// // test program int main( void ) { VALEN::mkFile pFile; if( pFile.openFile("/home/valen16/a.txt", VALEN::READONLY) ) { std::cout << "File opened..." << std::endl; } else { std::cout << "Couldn't open the file..." << std::endl; } if( pFile.IsReadOnly()) { std::cout << "File is read only" << std::endl; } else { std::cout << "Hmm it isn't..." << std::endl; } pFile.closeFile(); return 0; }
-
Vielleicht möchtest du dir einen Entwurf von Ali Abdolrahmani anschauen:
#include <cstdio> #include <string> /* File class - Ali Abdolrahmani, The University of Tehran */ namespace ALIAB { class File { private: FILE *fp; // File pointer public: // Constructors File(const char* filename, const char* filemode = "w+") { // fp = fopen(filename, filemode); fp = NULL; if (fp == NULL) throw "can't open file"; } // default constructor File() { fp = tmpfile(); // open temp file for read/write if (fp == NULL) throw "can't open file"; } // Destructor ~File() { fclose(fp); } FILE *getfp() const { return fp; } }; } /* namespace ALIAB */ int main() { using namespace ALIAB; try { File f("test1"); // open test1, read and write File g("test2", "w"); // open test2, write only File h; // open temp file, read and write File *p = new File; // open temp file, read and write File *q = new File("dbase"); // open dbase for read and write File *r = new File("records", "r"); // open records for read only delete p; // close temp file delete q; // close dbase delete r; // close records } catch (char* msg) { std::cerr << msg << std::endl; return 1; } return 0; }
-
wie wärs mit
enum { READ = 1, WRITE = 2, READWRITE = 3 };
dann gingen x.mayRead(), x.mayWrite() und derartige dinge...
(a == b) ? true : false
finde ich lächerlich