operator>> works in Visual C++ 2010 but not G++ on Linux
I have the following problem:
I have code that works fine with visual c++ 2010, but when I compile it on Linux, it get compiled, but something there doesn't work:
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 on an array of Complex
number, the operator>>
of the Complex
works fine.
The input is supposed to look like this:
[2+3i,9i,1]
When I run this code on 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]
the 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 the other Vector
btw: 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 is where i define the constructor of 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 anyone help me?
I assume the problem is in your call to in.sync()
.
This flushes the input buffer, ie it discards whatever data is currently in the istream's buffer. What exactly gets put into the buffer depends on a) your platform and b) what you did before you call operator>>
.
As for b), this is why it's wrong to call sync
from an operator>>
, but that's "your problem".
As for a), you should know that UNIX systems do line buffering for console text input at the operating system level, before istream gets a say. Data that you want to flush might be in the operating system buffer instead of in istream's buffer.
If you just want to skip whitespace, you should use in >> std::ws;