我有以下問題:操作>>作品在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;
}
誰能幫助我?
您可以包括你如何定義/宣告你的'VECTOR'對象?你是如何初始化'_n'的? – sarnold 2011-12-24 00:26:26
如果您進行了錯誤檢查 - 例如,您可能會獲得一些非常有用的信息。測試以確保每次讀取內容時數據流都沒有失敗,並檢查是否確實得到了'['當你認爲你正在閱讀它時。 – Hurkyl 2011-12-24 02:23:36
我做了這個錯誤檢查,它在Visual C++ 2010上工作正常,但它在Linux上失敗......並且我無法做這個錯誤檢查 – hudac 2011-12-24 08:21:21