Forum: PC-Programmierung Tamplate per Isstream füllen


von Kuso (Gast)


Lesenswert?

hab ne Templateklasse

template <class T> class Arrerganzung : public Arr<T>
{
public:
  int &operator[](int index)
   {
          if (index > 10) throw out_of_range("Index zu goß") ;
     return arr[index];
   }
   friend istream& operator>> (istream& s, Arrerganzung<T>& p);
};

und die wird so aufgerufen (in der main)

... arr[i] = daten;

i und daten möchte ich in einer eigenen stream klasse einlesen
und zwar so

istream& operator>>(istream& s,Arrerganzung<T> &p)
{

  s>> p.i >> p.daten; //oder mit Mem
  return s;
}

geht aber nicht ohne Template würde es gehen aber nicht als 
Templateklasse

von Rolf Magnus (Gast)


Lesenswert?

> i und daten möchte ich in einer eigenen stream klasse einlesen
> und zwar so

Ich sehe da keine eigene Klasse. Nur einen Operator.

> geht aber nicht ohne Template würde es gehen aber nicht als
> Templateklasse

Satzzeichen könnten hier Wunder wirken und dafür sorgen, daß man's nicht 
dreimal lesen muß, um es zu verstehen. Was heißt den "geht aber nicht"? 
Was hast du versucht? Was ist passiert? Was hätte stattdessen passieren 
sollen?

von Kuso (Gast)


Lesenswert?

es lässt sich nicht übersetzen
Template fehler

von Kuso (Gast)


Lesenswert?

Ich hab mal ein anderes Beispiel gemacht

was muss ich da noch ändern das es sich übersetzen lässt

#include <iostream>
#include <cstring>
#include <string>
#include <list>
using namespace std;
class Bundesland
{
public:Bundesland(char *bundesland = "") {this->bundesland = 
bundesland;}
protected:
  char *bundesland;
};
class Bank : public Bundesland
{
public:Bank(char *bank = "",char *bundesland = "") : 
Bundesland(bundesland)
     {this->bank = bank;}

protected:
   char *bank;


};
template <class T>
class Konto : public Bank
{
public: Konto(char *bank = "",char *bundesland = "", T konto= 0) : 
Bank(bank,bundesland)
    {this->konto = konto;}
    friend ostream &operator<<(ostream &os,Konto &a);
private: T konto;
};

ostream &operator<<(ostream &os,Konto<T> &a)
{
  return os << a.konto  << a.bank<< a.bundesland <<endl;
}
int main(void)
{

Konto <int>ko("sparkasse","bayern", 123);
cout << ko;
   return 0;
}

von Rolf Magnus (Gast)


Lesenswert?

Du mußt dem Compiler sagen, daß dein Operator ein Template sein soll. 
Dazu mußt du ihm das aber schon bei der friend-Deklaration sagen, wozu 
aber erst eine Forward-Deklaration für den Operator da sein muß, wofür 
man eine Forward-Deklaration der Klasse braucht. Sieht dann gesamt so 
aus:
1
#include <iostream>
2
#include <cstring>
3
#include <string>
4
#include <list>
5
using namespace std;
6
class Bundesland
7
{
8
public:Bundesland(const char *bundesland = "") {this->bundesland =
9
bundesland;}
10
protected:
11
  const char *bundesland;
12
};
13
class Bank : public Bundesland
14
{
15
public:Bank(const char *bank = "",const char *bundesland = "") :
16
Bundesland(bundesland)
17
     {this->bank = bank;}
18
19
protected:
20
   const char *bank;
21
22
23
};
24
25
template<class T> class Konto;
26
template <class T>ostream &operator<<(ostream &os,Konto<T> &a);
27
28
template <class T>
29
class Konto : public Bank
30
{
31
public: Konto(const char *bank = "",const char *bundesland = "", T konto= 0) :
32
Bank(bank,bundesland)
33
    {this->konto = konto;}
34
    friend ostream &operator<< <>(ostream &os,Konto<T> &a);
35
private: T konto;
36
};
37
38
template <class T>
39
ostream &operator<<(ostream &os,Konto<T> &a)
40
{
41
  return os << a.konto  << a.bank<< a.bundesland <<endl;
42
}
43
int main(void)
44
{
45
46
Konto <int>ko("sparkasse","bayern", 123);
47
cout << ko;
48
   return 0;
49
}

von Kuso (Gast)


Lesenswert?

das steht nirgends bei uns im Skript so ein mist

von Kuso (Gast)


Lesenswert?

aber einlesen geht aber totzdem net




#include <iostream>
#include <cstring>
#include <string>
#include <list>
using namespace std;
class Bundesland
{
public:Bundesland(const char *bundesland = "") {this->bundesland =
bundesland;}
protected:
  const char *bundesland;
};
class Bank : public Bundesland
{
public:Bank(const char *bank = "",const char *bundesland = "") :
Bundesland(bundesland)
     {this->bank = bank;}

protected:
   const char *bank;


};

template<class T> class Konto;
template <class T>ostream &operator<<(ostream &os,Konto<T> &a);

template <class T>
class Konto : public Bank
{
public: Konto(const char *bank = "",const char *bundesland = "", T 
konto= 0) :
Bank(bank,bundesland)
    {this->konto = konto;}
    friend ostream &operator<< <>(ostream &os,Konto<T> &a);
  friend istream& operator>>(istream& s,Konto<T> &p);
private: T konto;
};

template <class T>
ostream &operator<<(ostream &os,Konto<T> &a)
{
  return os << a.konto  << a.bank<< a.bundesland <<endl;
}

template <class T>
istream& operator>>(istream& s,Konto<T> &p)
{

  s >> p.konto ; //oder mit Mem
  return s;
}

int main(void)
{


Konto <int>ko("sparkasse","bayern", 123);
cout << ko;
Konto <double>li;
cin >> li;
//cout << li;
   return 0;
}

von Klaus W. (mfgkw)


Lesenswert?

im Skript steht bestimmt auch nicht, wie man eine Quelltext lesbar
zeigt (aber beim Schreiben hier im Forum steht es über dem
eingegebenen Text unter "Formatierung").
So kann das doch keiner lesen.
Und was heißt "geht aber totzdem net"? So klar redet meine Oma über
Motoren.

von Rolf Magnus (Gast)


Lesenswert?

Wie wäre es mal mit mitdenken? Wieso glaubst du, daß du die Deklaration 
deines input-Operator-Templates anders machen kannst als beim 
dazugehörigen output-Operator? Ist dir nicht aufgefallen, daß da genau 
dieselbe Fehlermeldung kommt? Oder liest du die gar nicht erst?

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.