Doofes Map Problem
-
Ich sitz jetzt seit ewigkeiten an so einem doofen Problem und ich verstehe es nicht!
Ich versuche im Prinzip folgendes...
map<const char*, int> Blub; class a { void funktion1() { Blub[foo]=100; } char *foo; }; class b { void funktion2() { cout<<Blub[foo2]; } char *foo2; };So, wenn jetzt in foo und foo2 GENAU das selbe steht dann wird mir beim aufrufen von funktion2 trotzdem 0 ausgegeben ???
Woran liegt das ? An diesem doofen const ? Wie behebe ich das ?AHHHH!
-
Nein, das liegt an dem doofen *.
Wie oft eigentlich noch? char * ist in C++ im Allgeminen kein geeigneter Typ zur Darstellung von Strings. Dafür nimmt man std::string. Und damit hätte dein Code auch funktioniert (er scheitert daran, daß die map nur die Adressen der C-Strings vergleicht und nicht die C-Strings).
-
Ok, das mit dem char* hatte ich eingebaut weil ich sicher gehen wollte das auch wirklich in beiden Strings das selbe steht .... ich hatte es nämlich vorher mit strings gemacht... da hats aber auch nicht funktioniert!
string test1="TEST"; string test2="TEST"; map<const char*, int> testor; testor[test1.c_str()]=100; cout<<testor[test2.c_str()]<<endl;So, da gibt er auch 0 aus ? Vergleicht er da jetzt auch nur die Adressen ? Wegen dem .c_str() ? Ohne gehts nicht ?
Edit: Oder meintest du auch das char*

map<const char*, int> testor;Wäre irgendwie logisch...
Aber wenn ich da String einsetze dann mault der Compiler rum ... bzw. er mault rum wenn ich dann da mittestor[test1]=100;was rein tun will...
------ Build started: Project: consolentestprog, Configuration: Debug Win32 ------ Compiling... main.cpp f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(1170) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(138) : while compiling class-template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const' with [ _Ty=std::string ] f:\Microsoft Visual Studio .NET 2003\Vc7\include\map(67) : see reference to class template instantiation 'std::less<_Ty>' being compiled with [ _Ty=std::string ] f:\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(23) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled with [ _Kty=std::string, _Ty=int, _Pr=std::less<std::string>, _Alloc=std::allocator<std::pair<const std::string,int>>, _Mfl=false ] f:\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(65) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled with [ _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> ] f:\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(87) : see reference to class template instantiation 'std::_Tree_ptr<_Traits>' being compiled with [ _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> ] f:\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(105) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled with [ _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> ] f:\Microsoft Visual Studio .NET 2003\Vc7\include\map(77) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled with [ _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> ] f:\Projekte\consolentestprog\main.cpp(390) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled with [ _Kty=std::string, _Ty=int ] f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(1170) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(1170) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(655) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(655) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(655) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\utility(73) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\utility(73) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' f:\Microsoft Visual Studio .NET 2003\Vc7\include\utility(73) : see declaration of 'std::operator`<'' f:\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139) : error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
-
map<std::string, int> testor;
ist korrekt. Aus der Fehlermeldung werde ich jetzt nicht ganz schlau. #include <string> hast du drin, ja? Im Zweifelsfall zeig mal ein Bißchen mehr Code.
Und nebenbei bemerkt, ich meine alle char *. Das für Strings zu benutzen ist abgesehen von sehr sehr wenigen Ausnahmen ein schweren Fehler. Ist viel zu umständlich und wenn man sich da nicht sehr genau auskennt und extrem sorgfältig arbeitet, geht das hundertprozentig schief (wie man an deinem Beispiel sieht).
-
Z2 schrieb:
...#include <string> hast du drin, ja? ...
Doofe Sache ... das wars
Und jetzt funktioniert es auch endlich...
Hatte mich schon damit abgefunden das es mit mit map und 2 verschiedenen Variablen nie funktionieren wird.Danke für die Hilfe!
