Immer mehr private bytes :(
-
Aha!
Nun, und wie setze ich dann während der Laufzeit std::string auf " "?
MfG
-
Damit meine ich natürlich
struct oldJoinedPlayer { std::string ip; std::string id; }; std::vector<oldJoinedPlayer> oldJoinedPlayer;Jedes Elemennt eines jeden oldJoinedPlayer Objekts auf " " setzen.
-
Hello schrieb:
Nun, und wie setze ich dann während der Laufzeit std::string auf " "?
Indem Du keine Literalkonstanten benutzt, sondern " " zur Laufzeit irgendwoher einliest und dann einen parametrisierten Konstruktor, oder den Zuweisungsoperator verwendest.
-
Hey!
Geht es denn nicht einfacher?
MfG
-
Ich möchte die string-Elemente immer wieder "leeren". Muss ich da dann doch eine Klasse draus machen, eine Memberfunktion schreiben und immer wieder für alle Elemente aufrufen?
-
Nein, du mußt die std::fill-Funktion verwenden, wie ich dir schon geschrieben habe. Und wenn du die Fehlermeldungen, die du bekommen hast, hier gepostet hättest, dann hätte man dir damit auch längst helfen können. Ich könnte deswegen schon wieder ausrasten, aber das habe ich gerade in einem anderen Thread getan. Also lassen wir es.
-
Ein struct darf auch Memberfunktionen haben.
struct oldJoinedPlayer { oldJoinedPlayer() { init(); } void init() { ip = ""; id = ""; } std::string ip; std::string id; }; std::vector<oldJoinedPlayer> oldJoinedPlayer;Dann std::fill verwenden, wie Z2 es schon beschrieben hat.
-
Herst ich hab die Fehlermeldung doch gepostet, als ich std::fill verwenden wollte.
-
Sieht für mich relativ überflüsig aus. Ein std::string ist bei seiner Konstruktion bereits ein leerer String.
-
Hmm versteh ich ned!
Also was jetzt, init() oder std::fill? Warum beides?
-
std::fill. init brauchst du in diesem Fall nicht.
Der Funktionsname ist sowieso nicht so toll gewählt, denn das init macht ja der Konstuktor (reset oder so wäre als Name eher angebracht). Aber ist hier ja egal.
-
Das mit dem init war nur, falls du hinterher irgendwann die Elemente wieder auf den Anfangswert zurücksetzen willst, ansonsten ist es nicht nötig.
-
Sinds inzwischen schon weniger private bytes?
-
@Konstruktör: Beim Zurücksetzen mit std::fill macht das nicht viel Sinn. Aber man könnte natürlich statt std::fill auch deine init-Methode (obwohl ich den Namen immer noch nicht mag) in Verbindung mit std::for_each verwenden. Wie auch immer, ich denke das wird für den Fragen wohl zuviel sein. Mit der einfachen std::fill-Variante dürfte er wesentlich besser aufgehoben sein.
-
Ich weiß noch immer nicht, wie ich std::fill richtig einsetze.
Nicht mit einer Literalkonstanten, also so?std::string a; std::fill(oldJoinedPlayer.begin(), oldJoinedPlayer.end(), a); std::fill(joinedPlayer.begin(), joinedPlayer.end(), a);1>------ Build started: Project: SlowBob, Configuration: Release Win32 ------ 1>Compiling... 1>SlowBob.cpp 1>C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2726) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion) 1> e:\c++\gold\slowbob\slowbob\SlowBob.h(57): could be 'SlowBob::oldJoinedPlayer &SlowBob::oldJoinedPlayer::operator =(const SlowBob::oldJoinedPlayer &)' 1> while trying to match the argument list '(SlowBob::oldJoinedPlayer, const std::string)' 1> C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2754) : see reference to function template instantiation 'void std::_Fill<std::_Vector_iterator<_Ty,_Alloc>,std::basic_string<_Elem,_Traits,_Ax>>(_FwdIt,_FwdIt,const std::basic_string<_Elem,_Traits,_Ax> &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::oldJoinedPlayer, 1> _Alloc=std::allocator<SlowBob::oldJoinedPlayer>, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Ax=std::allocator<char>, 1> _FwdIt=std::_Vector_iterator<SlowBob::oldJoinedPlayer,std::allocator<SlowBob::oldJoinedPlayer>> 1> ] 1> .\SlowBob.cpp(62) : see reference to function template instantiation 'void std::fill<std::_Vector_iterator<_Ty,_Alloc>,std::string>(_FwdIt,_FwdIt,const std::basic_string<_Elem,_Traits,_Ax> &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::oldJoinedPlayer, 1> _Alloc=std::allocator<SlowBob::oldJoinedPlayer>, 1> _FwdIt=std::_Vector_iterator<SlowBob::oldJoinedPlayer,std::allocator<SlowBob::oldJoinedPlayer>>, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Ax=std::allocator<char> 1> ] 1>C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2726) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion) 1> e:\c++\gold\slowbob\slowbob\SlowBob.h(69): could be 'SlowBob::joinedPlayer &SlowBob::joinedPlayer::operator =(const SlowBob::joinedPlayer &)' 1> while trying to match the argument list '(SlowBob::joinedPlayer, const std::string)' 1> C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2754) : see reference to function template instantiation 'void std::_Fill<std::_Vector_iterator<_Ty,_Alloc>,std::basic_string<_Elem,_Traits,_Ax>>(_FwdIt,_FwdIt,const std::basic_string<_Elem,_Traits,_Ax> &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::joinedPlayer, 1> _Alloc=std::allocator<SlowBob::joinedPlayer>, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Ax=std::allocator<char>, 1> _FwdIt=std::_Vector_iterator<SlowBob::joinedPlayer,std::allocator<SlowBob::joinedPlayer>> 1> ] 1> .\SlowBob.cpp(63) : see reference to function template instantiation 'void std::fill<std::_Vector_iterator<_Ty,_Alloc>,std::string>(_FwdIt,_FwdIt,const std::basic_string<_Elem,_Traits,_Ax> &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::joinedPlayer, 1> _Alloc=std::allocator<SlowBob::joinedPlayer>, 1> _FwdIt=std::_Vector_iterator<SlowBob::joinedPlayer,std::allocator<SlowBob::joinedPlayer>>, 1> _Elem=char, 1> _Traits=std::char_traits<char>, 1> _Ax=std::allocator<char> 1> ] 1>Build log was saved at "file://e:\C++\Gold\SlowBob\SlowBob\Release\BuildLog.htm" 1>SlowBob - 2 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-
struct oldJoinedPlayer { std::string ip; std::string id; }; std::vector<oldJoinedPlayer> oldJoinedPlayer; struct joinedPlayer { std::string ip; std::string id; }; std::vector<joinedPlayer> joinedPlayer;Liegts vielleicht an der Namensgebung?
-
Du hast in deinem std::vector keine Strings drin stehen. Da stehen struct's drin. Also mußt du std::fill auch eine struct übergeben.
-
Hab ich mir auch schon gedacht. Nur funktioniert's nicht:
struct emptyPlayer { std::string ip; std::string id; }; emptyPlayer emptyPlayer; std::fill(oldJoinedPlayer.begin(), oldJoinedPlayer.end(), emptyPlayer); std::fill(joinedPlayer.begin(), joinedPlayer.end(), emptyPlayer);1>------ Build started: Project: SlowBob, Configuration: Release Win32 ------ 1>Compiling... 1>SlowBob.cpp 1>C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2726) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const SlowBob::{ctor}::emptyPlayer' (or there is no acceptable conversion) 1> e:\c++\gold\slowbob\slowbob\SlowBob.h(51): could be 'SlowBob::oldJoinedPlayer &SlowBob::oldJoinedPlayer::operator =(const SlowBob::oldJoinedPlayer &)' 1> while trying to match the argument list '(SlowBob::oldJoinedPlayer, const SlowBob::{ctor}::emptyPlayer)' 1> C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2754) : see reference to function template instantiation 'void std::_Fill<std::_Vector_iterator<_Ty,_Alloc>,SlowBob::{ctor}::emptyPlayer>(_FwdIt,_FwdIt,const SlowBob::{ctor}::emptyPlayer &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::oldJoinedPlayer, 1> _Alloc=std::allocator<SlowBob::oldJoinedPlayer>, 1> _FwdIt=std::_Vector_iterator<SlowBob::oldJoinedPlayer,std::allocator<SlowBob::oldJoinedPlayer>> 1> ] 1> .\SlowBob.cpp(66) : see reference to function template instantiation 'void std::fill<std::_Vector_iterator<_Ty,_Alloc>,SlowBob::{ctor}::emptyPlayer>(_FwdIt,_FwdIt,const SlowBob::{ctor}::emptyPlayer &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::oldJoinedPlayer, 1> _Alloc=std::allocator<SlowBob::oldJoinedPlayer>, 1> _FwdIt=std::_Vector_iterator<SlowBob::oldJoinedPlayer,std::allocator<SlowBob::oldJoinedPlayer>> 1> ] 1>C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2726) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const SlowBob::{ctor}::emptyPlayer' (or there is no acceptable conversion) 1> e:\c++\gold\slowbob\slowbob\SlowBob.h(57): could be 'SlowBob::joinedPlayer &SlowBob::joinedPlayer::operator =(const SlowBob::joinedPlayer &)' 1> while trying to match the argument list '(SlowBob::joinedPlayer, const SlowBob::{ctor}::emptyPlayer)' 1> C:\Programme\Microsoft Visual Studio 8\VC\include\xutility(2754) : see reference to function template instantiation 'void std::_Fill<std::_Vector_iterator<_Ty,_Alloc>,SlowBob::{ctor}::emptyPlayer>(_FwdIt,_FwdIt,const SlowBob::{ctor}::emptyPlayer &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::joinedPlayer, 1> _Alloc=std::allocator<SlowBob::joinedPlayer>, 1> _FwdIt=std::_Vector_iterator<SlowBob::joinedPlayer,std::allocator<SlowBob::joinedPlayer>> 1> ] 1> .\SlowBob.cpp(67) : see reference to function template instantiation 'void std::fill<std::_Vector_iterator<_Ty,_Alloc>,SlowBob::{ctor}::emptyPlayer>(_FwdIt,_FwdIt,const SlowBob::{ctor}::emptyPlayer &)' being compiled 1> with 1> [ 1> _Ty=SlowBob::joinedPlayer, 1> _Alloc=std::allocator<SlowBob::joinedPlayer>, 1> _FwdIt=std::_Vector_iterator<SlowBob::joinedPlayer,std::allocator<SlowBob::joinedPlayer>> 1> ] 1>Build log was saved at "file://e:\C++\Gold\SlowBob\SlowBob\Release\BuildLog.htm" 1>SlowBob - 2 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-
emptyPlayer emptyPlayer?
Du kannst ein Objekt nicht genauso nennen wie eine Klasse. Und im übrigen solltest du zum Löschen auch keine eigene struct anlegen.
-
Hey!
"Und im übrigen solltest du zum Löschen auch keine eigene struct anlegen."
Warum nicht? Wie sonst?
MfG