Methoden überladen Problem
-
Ich hab irgendwie n kleines Problem mit Methoden überladen, der Compiler meint das es nicht "eindeutig" ist. Aber warum ist es nicht eindeutig? Was muss ich machen damit es eindeutig wird. Ich will das man meine Methode in der Form aufrufen kann "read_in(1222);" ohne Typumwandlung....(wenn das irgendwie geht
). Aber warum nicht "eindeutig"?
Hier der Code:streamwork.hpp
#include <fstream> #include <string> namespace sw { enum ifof {reading,writing}; class Streamwork { public: Streamwork(const std::string &file_name, ifof mode, bool clear=true); ifof mod() const; //derzeitiger modus? bool lg() const; //looking good? bool read_in(unsigned short value); bool read_in(unsigned long value); bool read_out(unsigned short save) {}; //stub bool read_out(unsigned long save) {}; //stub private: bool open_stream(const std::string &file_name, ifof mode, bool clear=true); void close_stream(); bool ch_mod(); //change mode const std::string m_file_name; ifof m_mode; std::fstream m_file; }; }streamwork.cpp
(ganz unten letzte beide Methoden)#include "streamwork.hpp" #include <string> #include <fstream> #include <iostream> using namespace std; namespace sw { Streamwork::Streamwork(const std::string &file_name, ifof mode, bool clear) :m_file_name(file_name),m_mode(mode) { open_stream(file_name,mode,clear); } bool Streamwork::open_stream(const string &file_name, ifof mode, bool clear) { if(clear) m_file.open(file_name.c_str(),(mode) ? (ios::binary | ios::in) : (ios::binary | ios::out)); else m_file.open(file_name.c_str(),(mode) ? (ios::binary | ios::in | ios::ate) : (ios::binary | ios::out | ios::app)); return m_file; } void Streamwork::close_stream() { m_file.close(); } ifof Streamwork::mod() const { return m_mode; } bool Streamwork::lg() const { return m_file; } bool Streamwork::ch_mod() { close_stream(); if((m_mode+1) > 1) m_mode=reading; else m_mode=writing; return open_stream(m_file_name,m_mode); } //UM DIE BEIDEN METHODEN GEHT ES bool Streamwork::read_in(unsigned short value) //!!! { std::cout << "JETZT KOMMEN 2 BYTE!\n"; if(m_mode != reading) ch_mod(); m_file.write(reinterpret_cast<char*>(&value),sizeof(unsigned short)); //2 Byte return m_file; } bool Streamwork::read_in(unsigned long value) //!!! { std::cout << "JETZT KOMMEN 4BYTE!\n"; if(m_mode != reading) ch_mod(); m_file.write(reinterpret_cast<char*>(&value),sizeof(unsigned long)); //4 Byte return m_file; } }main.cpp
#include <iostream> #include "streamwork.hpp" using namespace sw; int main() { Streamwork test("H:/test.oop",reading,false); unsigned short a=13353; test.read_in(a); //Warum gehts nur so? test.read_in(1200); //Und nicht so? return 0; }MfG
Stromberg
-
Der Compiler sieht bei deiner Konstante ja nur einen int Wert, und weiß nicht ob er ihn in ein short oder in einer long umwandeln soll.
Abhilfe schafft ein Postfix für deine Zahl wie z.b.:
75 // int 75u // unsigned int 75l // long 75ul // unsigned long
-
Und was ist das Kürzel bei short? Bei "us" kommt bei mir n Fehler...inalid suffix.
#include <iostream> #include "streamwork.hpp" using namespace sw; int main() { Streamwork test("H:/test.oop",reading,false); unsigned short a=13353; test.read_in(a); //Warum gehts nur so? test.read_in(1200us); //Und nicht so? return 0; }MfG
Stromberg
-
Gibts für "short" gar kein Kürzel?
MfG
Stromberg
-
http://msdn2.microsoft.com/en-us/library/c70dax92.aspx
http://msdn2.microsoft.com/en-us/library/00a1awxf.aspx
-
Ich finde da nichts über short suffixes.
MfG
Stromberg
-
Gibts also keine?
MfG
Stromberg
-
Und ein static_cast<> ist Dir zu unsauber?