ich habe jetzt schon einige stunden mit folgendem beispielprogramm und der erzeugten fehlermeldung beim kompilieren zugebracht.
Code: Alles auswählen
#include <iostream>
using namespace std;
class myComplex
{
private:
double _real;
double _image;
public:
myComplex(double val1=0.0, double val2=0.0)
{
_real = val1;
_image = val2;
}
void print_Complex()
{
cout << _real << " + " << _image << '\n';
}
friend ostream& operator<<(ostream& os, const myComplex& val);
friend istream& operator>>(ostream& is, const myComplex& val);
};
ostream& operator<<(ostream& os, const myComplex val)
{
os << val._real << " + " << val._image << '\n';
return os;
}
istream& operator>>(istream& is, const myComplex val)
{
cout << "Real Wert: ";
is >> val._real;
cout << "Imaginaer Wert: ";
is >> val._image;
}
int main()
{
myComplex val1, val2, val3;
cout << "Komplexe Zahl eingeben:";
cin >> val1;
cout << val1;
cout << "2 komplexe Zahlen eingeben:";
cin >> val2 >> val3;
cout << val2 << val3;
return 0;
}
Code: Alles auswählen
g++ -Wall complex2.cpp -o complex2
complex2.cpp: In function 'std::ostream& operator<<(std::ostream&, myComplex)':
complex2.cpp:7: error: 'double myComplex::_real' is private
complex2.cpp:24: error: within this context
complex2.cpp:8: error: 'double myComplex::_image' is private
complex2.cpp:24: error: within this context
complex2.cpp: In function 'std::istream& operator>>(std::istream&, myComplex)':
complex2.cpp:7: error: 'double myComplex::_real' is private
complex2.cpp:30: error: within this context
complex2.cpp:8: error: 'double myComplex::_image' is private
complex2.cpp:32: error: within this context
complex2.cpp: In function 'int main()':
complex2.cpp:40: error: ambiguous overload for 'operator<<' in 'std::cout << val1'
complex2.cpp:22: note: candidates are: std::ostream& operator<<(std::ostream&, myComplex)
complex2.cpp:19: note: std::ostream& operator<<(std::ostream&, const myComplex&)
complex2.cpp:43: error: ambiguous overload for 'operator<<' in 'std::cout << val2'
complex2.cpp:22: note: candidates are: std::ostream& operator<<(std::ostream&, myComplex)
complex2.cpp:19: note: std::ostream& operator<<(std::ostream&, const myComplex&)
kann mir eventuell jemand erklären bzw. eine richtung aufzeigen wo ich lang muss. mit anderen als den shift operatoren gibts es das problem hier nicht.