2011-12-24 31 views
4

我有以下問題:操作>>作品在Visual C++ 2010,但不是G ++在Linux上

我有一個用Visual C++ 2010, 但是當我編譯它在Linux上,它被編譯工作正常碼,但事有不工作:

這是我Vector輸入operator>>:中Complex數陣列上

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點, 的的工作正常。

輸入應該是這樣的:

[2+3i,9i,1] 

當我運行的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] 

當我運行相同代碼在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] 

現在我不能ev恩寫其他Vector

BTW: 這是我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 

這裏是我定義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; 
} 

誰能幫助我?

+4

您可以包括你如何定義/宣告你的'VECTOR'對象?你是如何初始化'_n'的? – sarnold 2011-12-24 00:26:26

+0

如果您進行了錯誤檢查 - 例如,您可能會獲得一些非常有用的信息。測試以確保每次讀取內容時數據流都沒有失敗,並檢查是否確實得到了'['當你認爲你正在閱讀它時。 – Hurkyl 2011-12-24 02:23:36

+0

我做了這個錯誤檢查,它在Visual C++ 2010上工作正常,但它在Linux上失敗......並且我無法做這個錯誤檢查 – hudac 2011-12-24 08:21:21

回答

6

我認爲問題出在您撥打in.sync()

這會刷新輸入緩衝區,即丟棄當前在istream緩衝區中的任何數據。究竟是什麼進入緩衝區取決於:a)您的平臺和b)您在致電operator>>之前做了什麼。

至於b),這就是爲什麼從operator>>調用sync是錯誤的,但那是「你的問題」。

對於a),您應該知道UNIX系統在操作系統級別對控制檯文本輸入進行行緩衝,然後istream會說話。您要刷新的數據可能在操作系統緩衝區中,而不是在istream的緩衝區中。

如果你只是想跳過空格,你應該使用in >> std::ws;

+0

非常感謝。 我知道它有in.sync ... – hudac 2011-12-24 10:59:50