C++, Probleme mit String kopieren und operator

Kümmelkorn

Aktives Mitglied
Thread Starter
Dabei seit
06.10.2008
Beiträge
1.941
Reaktionspunkte
127
Hallöle,

ich habe eben angefangen, endlich wieder ernsthaft mit C++ anzufangen. Da ich C und Java schon "kann", dachte ich, dass es eigentlich gar nicht so schwer sein könne.. Tja. :rolleyes:

Hab mal ein kleines OOP-HalloWelt-Beispiel gebastelt:
Code:
#ifndef MyProgramm
#define MyProgramm

#include <iostream>
#include <string>

class HalloWelt {
	public:
		HalloWelt(const std::string& t);
		std::string getText() const;
		friend std::ostream& operator<< (std::ostream&, HalloWelt);
	private:
		std::string text;
};

HalloWelt::HalloWelt(const std::string &t) {
	text = t;
}

std::string HalloWelt::getText() const {
	return text;
}

std::ostream& operator<< (std::ostream& out, HalloWelt hw) {
	return out << hw.getText();
}
#endif


Damit habe ich noch 2 Probleme:

1. getText (Getter für einen String) liefert mir bisher afaik die direkte Instanz von einer privaten Variablen -> schlecht! Allerdings finde ich keine ordentliche copy() Funktion in C++ :eek: Irgendwie finde ich über Google nur strcpy (das ist C) und copy, was aber bei 3 Parametern auch eher an C erinnert. Gibts da nix besseres?

2. Ich möchte << überladen. Leider liefert mir meine operator<< nur die Adresse des Textes. Dabei meine ich, alles richtig gemacht zu haben. Kann mir jemand erkären, warum das falsch ist und wie ich es verbessere?



Gruß, Micha

PS: Ich hab die Header-Datei von der CPP eigentlich sauber getrennt, aber ich wollte es fürs Forum etwas kürzer schreiben
 
Zuletzt bearbeitet:
warum kein using namespace std;?

HalloWelt::HalloWelt(const std::string &t) {
text = t; <<-- wenn ich das richtig verstehe, weist du hier die adresse als wert zu...
}
 
mh, "using namespace xyz" macht namespaces nutzlos. Aber stimmt, bei dem Beispiel ists egal, habs geändert.

Hab deinen Hinweis mal verfolgt und stattdessen eine Methode "assign()" gefunden. Klappt aber leider immer noch nicht :(

Witzigerweise funktioniert es, wenn ich cout << hallowelt->getText() << endl; aufrufe. Aber ohne Methodenaufruf funktioniert es nicht?!

Hab die wichtigsten Änderungen rot markiert:

Code:
// ...
using namespace std;

class HalloWelt {
	public:
		[COLOR="red"]HalloWelt(const string t); // statt &t[/COLOR]
		string getText() const;
		friend ostream& operator<< (ostream&, [COLOR="red"]HalloWelt&[/COLOR]) // statt HalloWelt (ohne &);
	private:
		string text;
};

HalloWelt::HalloWelt([COLOR="red"]const string t[/COLOR]) {
	[COLOR="red"]text.assign(t)[/COLOR]; // statt text = t
}

string HalloWelt::getText() const {
	return text;
}

ostream& operator<< (ostream& out, [COLOR="red"]HalloWelt& hw[/COLOR]) {
	return out << hw.getText(); // warum auch immer hw->getText() nicht will!?
}
#endif
 
Zuletzt bearbeitet:
:eek: hab den Fehler gefunden. meine main-Methode sah so aus:

Code:
int main() {
HalloWelt* hallowelt= new HalloWelt("hi");
cout << hallowelt << endl;
return 0;
}


Ähm.. ja.. was so ein fehlendes Sternchen nicht alles auslösen kann... :shame:
nächstes mal poste ich doch besser den ganzen Quellcode :hum:

Sorry und ein großes Danke, dass du trotzdem alles versucht hast! :)

Gruß, Micha
 
@Kümmelkorn

Nur ein paar kleine Hinweise damit Du Dir das nicht erst einprägst:
1. benutze die Initialisiererliste:
Code:
  HalloWelt::HalloWelt(const std::string &t) 
  :text(t)
  {
  }
Du solltest in der Liste die Variablen in der Reihenfolge Deiner Deklaration eintragen.

2. Rückgaben als const Referenz:
Code:
  const std::string& HalloWelt::getText() const {
	return text;
  }
sonst machst Du eine unnötige Kopie. Als inline Methode könnte es der Compiler
optimieren.

3. der friend '<<' operator?? Ich würde ihn als freie Methode implementieren:
Code:
  std::ostream& operator << ( std::ostream &rOut, const HalloWelt &rValue )
  {
    return rOut << rValue.getText() << std::endl;
  }
'friend' sollte sparsam eingesetzt werden (ist halt eine extrem starke Bindung)

Syntaxfehler darfst Du gern behalten.:)

leftshift
 
Zurück
Oben Unten