The >> operator works in Visual C ++ 2010, but not g ++ in Linux

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] //this is the input [6.105e-317+6.105e-317i,6.105e-317+6.105e-317i] [6.105e-317+6.105e-317i,6.105e-317+6.105e-317i] 

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?

+4
source share
1 answer

I assume the problem is in your in.sync() call.

This clears the input buffer, i.e. it discards all the data in the current buffer. What exactly fits into the buffer depends on: a) your platform and b) what you did before you called operator>> .

As for b), therefore it is wrong to call sync from operator>> , but this is "your problem".

As with a), you should be aware that UNIX systems execute line buffering for text input to the console at the operating system level before istream receives a response. The data you want to flush may be in the operating system buffer, and not in the istream buffer.

If you just want to skip spaces, you should use in >> std::ws;

+6
source

Source: https://habr.com/ru/post/1387824/


All Articles