运算符>>在Visual C++ 2010中工作,但在Linux上不能使用G++。
operator>> works in Visual C++ 2010 but not G++ on Linux
我有以下问题:
我的代码与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
数数组上,Complex
的operator>>
工作正常
输入应该是这样的:
[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
顺便说一句:这是向量。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),这就是为什么从operator>>
调用sync
是错误的,但这是"你的问题"。
对于a),您应该知道UNIX系统在istream获得发言权之前,在操作系统级别为控制台文本输入执行行缓冲。您想要刷新的数据可能在操作系统缓冲区中,而不是在istream的缓冲区中。
如果你只是想跳过空白,你应该使用in >> std::ws;
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的for循环不能正确获取argv
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么模板类中的对象不能返回值
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 数组长度,为什么从命令行获取时不能使用它?
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 为什么我不能将 rand() 与数组的大小一起使用?
- 为什么虚函数不能是静态的和全局的?
- 为什么我不能在 while 循环中创建线程?
- 为什么这个音频包络不能通过开关的情况?
- ``这个''不能用this-&gt;指针变量