operator >>适用于Visual C++ 2010,但不适用于Linux上的G ++

hud*_*dac 4 c++ visual-c++

我有以下问题:

我的代码在visual c ++ 2010中运行良好,但是当我在Linux上编译它时,它会被编译,但有些东西不起作用:

这是我的Vector意见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;
}
Run Code Online (Sandbox Code Playgroud)

_vector点的阵列上Complex号,operator>>该的Complex优良工程.

输入应该如下所示:

[2+3i,9i,1]
Run Code Online (Sandbox Code Playgroud)

当我在visual c ++ 2010上运行此代码时,它可以正常工作,如下所示:

cin>>v; // [1+1i,2]
cin>>u; // [10,5i]
cout<<v<<endl; //prints: [1+i,2]
cout<<u<<endl; //prints: [10,5i]
Run Code Online (Sandbox Code Playgroud)

当我在Linux上运行相同的代码时,在第一个数组之后[1+1i,2]程序结束:

[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]
Run Code Online (Sandbox Code Playgroud)

现在我甚至不能写另一个 Vector

顺便说一下:这是我的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
Run Code Online (Sandbox Code Playgroud)

这里是我定义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;
}
Run Code Online (Sandbox Code Playgroud)

谁能帮我?

wol*_*ang 6

我认为问题出在你的电话中in.sync().

这会刷新输入缓冲区,即它会丢弃当前在istream缓冲区中的任何数据.准确放入缓冲区取决于a)您的平台和b)您在致电之前所做的事情operator>>.

至于b),这就是为什么sync从a 打电话是错误的operator>>,但这是"你的问题".

至于a),您应该知道UNIX系统在操作系统级别对控制台文本输入执行行缓冲,然后才能获得声明.要刷新的数据可能位于操作系统缓冲区中,而不是在istream的缓冲区中.

如果你只想跳过空格,你应该使用 in >> std::ws;