Tamplate per Isstream füllen

Gast #1314667
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
Gast #1314866
> 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?
Gast #1315041
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;
}
Gast #1315061
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
}
Gast #1315073
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;
}
#1315206
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.
Gast #1316060
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?

Antwort schreiben

Eine eigene Frage? Dieser Thread ist über 17 Jahre alt. Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.

Neuen Thread starten

Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.