Zugriffsproblem innerhalb einer Klasse
-
Hallo!
Ich habe folgenden Code:
Header:#include<string> #include<iostream> #include<fstream> #include<list> class cConfigEntry { public: ~cConfigEntry() { } private: cConfigEntry() { } std::string KWord; void setKeyword(std::string _Keyword); std::string getKeyword() const; void setValue(std::string _Value); std::string getValue() const; std::string Val; Sort SortHow; bool operator<(const cConfigEntry& _Entry1,const cConfigEntry& _Entry2); bool operator==(const cConfigEntry& _Entry1,const cConfigEntry& _Entry2); friend class cConfigFile; };Definition:
void cConfigEntry::setKeyword(string _Keyword) { KWord=_Keyword; return; } string cConfigEntry::getKeyword() const { return KWord; } void cConfigEntry::setValue(string _Value) { Val=_Value; return; } string cConfigEntry::getValue() const { return Val; } bool operator<(const cConfigEntry& _Entry1,const cConfigEntry& _Entry2) { if((std::strcmp((_Entry1.KWord).c_str(),(_Entry2.KWord).c_str())==-1)&&(cConfigFile.SortHow==BY_KEYWORD)) { return true; } return false; } bool operator==(const cConfigEntry& _Entry1,const cConfigEntry& _Entry2) { if((!std::strcmp((_Entry1.Keyword).c_str(),(_Entry2.Keyword).c_str()))&&(cConfigFile.SortHow==BY_KEYWORD)) { return true; } return false; }Mein Compiler meckert, dass std::string KWord privat sei.
Ich greife doch auf einen Member eines Objekts, derselben Klasse zu.
Da muss ich doch auch Zugriff auf die privaten haben.
Warum geht das nicht?
-
Der Stringvergleich ist echt mal Code of the Day.
Ansonsten hat der Compiler natürlich Recht, es ist privat lol...
-
Naja, deine Operator-Definitionen sind ja auch außerhalb des Klassenscopes.
-
Entenwickler schrieb:
Der Stringvergleich ist echt mal Code of the Day.
Ansonsten hat der Compiler natürlich Recht, es ist privat lol...
Wieso, was ist denn an dem Stringvergleich auszusetzen? Abgesehen davon,
dass der Rest des Codes nicht funktioniert.

-
#if !defined(CONFIG_ENTRY_H__INCLUDED) #define CONFIG_ENTRY_H__INCLUDED #include <string> class ConfigEntry { private: ConfigEntry() {} public: ~ConfigEntry() {} bool operator < (const ConfigEntry& entry); bool operator == (const ConfigEntry& entry); public: void set_keyword(const std::string&); void set_value(const std::string&); std::string get_keyword() const; std::string get_value() const; private: std::string m_keyword; std::string m_value; }; #endif // CONFIG_ENTRY_H__INCLUDED#include "ConfigEntry.h" void ConfigEntry::set_keyword(const std::string& keyword) { m_keyword = keyword; } std::string ConfigEntry::get_keyword() const { return m_keyword; } void ConfigEntry::set_value(const std::string& value) { m_value = value; } std::string ConfigEntry::get_value() const { return m_value; } bool ConfigEntry::operator<(const ConfigEntry& entry) { return (m_keyword < entry.m_keyword); } bool ConfigEntry::operator == (const ConfigEntry& entry) { return (m_keyword == entry.m_keyword); }Das std::string selbst schon == implementiert hat usw ... hab deinen Filekram jetzt mal raus gelassen ... aber das wirst de ja noch selbst wieder rein bekommen ...
-
deine vergleichsoperatoren sind global definiert. d.h. außerhalb von deiner Klasse. Du musst die Operatoren als friend in der Klasse deklarieren, dann sollte es gehen.
Zum stringvergleich... *gg*
was meinst du wozu der std::string::operator<, operator>, == und != da sind?
-
Maxi schrieb:
deine vergleichsoperatoren sind global definiert. d.h. außerhalb von deiner Klasse. Du musst die Operatoren als friend in der Klasse deklarieren, dann sollte es gehen.
Zum stringvergleich... *gg*
was meinst du wozu der std::string::operator<, operator>, == und != da sind?Da hast du natürlich recht. Ich wusste gar nicht, dass die string-Klasse
solche Operatoren bereitstellt. Warum einfach, wenn's auch kompliziert geht?
Dann haben sich die Probleme wohl von slebst gelöst.
Ich muss dazusagen, ich bin blutiger Anfänger.