I have the following problem:
I have code that works fine with visual C ++ 2010, but when I compile it on Linux, it compiles, but something doesn't work there:
this is my Vector input operator>> :
istream& operator>>(istream& in,Vector& x) { char a; in.sync(); a=in.get(); //gets the '[' for(int i=0;i<x._n;i++) { in>>x._vector[i]; if ((i+1)!=x._n) a=in.get(); //gets the ',' } in>>a; //gets the ']' return in; }
_vector points to an array of Complex , operator>> Complex works fine.
The input should look like this:
[2+3i,9i,1]
When I run this code in Visual C ++ 2010, it works and looks like this:
cin>>v; // [1+1i,2] cin>>u; // [10,5i] cout<<v<<endl; //prints: [1+i,2] cout<<u<<endl; //prints: [10,5i]
When I run the same code on Linux, after the first array [1+1i,2] program ends:
[1+1i,2]
Now I canβt even write another Vector
By the way: this is my Vector.h
#ifndef _VECTOR_ #define _VECTOR_ #include <iostream> using namespace std; #include "Complex.h" class Vector { private: int _n; Complex *_vector; //points on the array of the complex numbers public: Vector(int n); // "Vector" - constructor of Vector with n instants Vector(const Vector& x); // "Vector" - copy constructor of Vector with n instants ~Vector(); // "~Vector" - destructor of Vector const Vector& operator=(const Vector& x); // "operator=" - operates "=" for Vector Complex& operator[](const int index); // "operator[]" - choose an instant by his index in the _vector const Vector operator+(const Vector& x) const; // "operator+" - operates "+" between two vectors const Vector operator-(const Vector& x) const; // "operator-" - operates "-" between two vectors const Vector operator*(double scalar) const; // "operator*" - multiplate all of the instants of the vector by the scalar friend const Vector operator*(double scalar,const Vector& x); // "operator*" - multiplate all of the instants of the vector by the scalar const Complex operator*(const Vector& x) const; // "operator*" - operates "*" between two vectors const Vector& operator+=(const Vector& x); // "operator+=" - operates "+=" for the instant const Vector& operator-=(const Vector& x); // "operator-=" - operates "-=" for the instant friend ostream& operator<<(ostream& out,const Vector& x); // "operator<<" - prints the vector friend istream& operator>>(istream& in,Vector& x); // "operator<<" - gets the vector const double operator!() const; // "operator!" - returns the the instant in the definite value of the vactor that his definite value is the highest (in the Vector) }; #endif
and here I define the constructor Vector: Vector.cpp
#include <iostream> using namespace std; #include <math.h> #include "Complex.h" #include "Vector.h" // "Vector" - constructor of Vector with n instants Vector::Vector(int n) { _vector=new Complex[n]; //new vector (array) of complex classes _n=n; }
Can anybody help me?