struct constructor + function parameter
- by Oops
Hi,
I am a C++ beginner. I have the following code, the reult is not what I expect. The question is why, resp. what is wrong. For sure, the most of you see it at the first glance.
struct Complex {
float imag;
float real;
Complex( float i, float r) {
imag = i;
real = r;
}
Complex( float r) {
Complex(0, r);
}
std::string str() {
std::ostringstream s;
s << "imag: " << imag << " | real: " << real << std::endl;
return s.str();
}
};
class Complexes {
std::vector<Complex> * _complexes;
public:
Complexes(){
_complexes = new std::vector<Complex>;
}
void Add( Complex elem ) {
_complexes->push_back( elem );
}
std::string str( int index ) {
std::ostringstream oss;
Complex c = _complexes->at(index);
oss << c.str();
return oss.str();
}
};
int main(){
Complexes * cs = new Complexes();
//cs->Add(123.4f);
cs->Add(Complex(123.4f));
std::cout << cs->str(0); return 0; }
for now I am interested in the basics of c++ not in the complexnumber theory ;-)
it would be nice if the "Add" function does also accept one real (without an extra overloading) instead of only a Complex-object is this possible?
many thanks in advance
Oops