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

标签 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;
}

_vector 指向一个 Complex 数组, Complexoperator>> 工作正常。

输入应该是这样的:

[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]

现在我什至不能写另一个 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

这里是我定义 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;
}

谁能帮帮我?

最佳答案

我假设问题出在您调用 in.sync() 的电话中.

这会刷新输入缓冲区,即它会丢弃当前在 istream 缓冲区中的任何数据。究竟放入缓冲区的内容取决于 a) 你的平台和 b) 你在调用 operator>> 之前做了什么.

至于b),这就是为什么调用sync是错误的原因来自 operator>> ,但那是“你的问题”。

至于 a),您应该知道,在 istream 获得发言权之前,UNIX 系统会在操作系统级别为控制台文本输入做行缓冲。您要刷新的数据可能在操作系统缓冲区中,而不是在 istream 的缓冲区中。

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

关于c++ - operator>> 适用于 Visual C++ 2010 但不适用于 Linux 上的 G++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8621784/

相关文章:

c++ - 如何在给定数组的任何子数组(任何大小)中找到最大值(或最小值)?

c++ - 为什么 stdin 和 stdout 看起来可以互换?

visual-c++ - 如何将 guid 转换为 *char

c++ - Visual C++ 6 变量作用域错误

c++ - 为什么 std::set<K, C, A>::erase 不采用 const_iterator?

c++ - 基于客户端服务器的多人游戏的最佳更新频率

c++ - c++中的模板类和friend关键字(具体例子引用boost::multi_index)

c++ - 对象数组融合的通用函数

c++ - 将 vector 模拟为二维数组

c++ - 如何删除 vector 中与另一个 vector 中的某些元素匹配的元素