【发布时间】:2015-09-27 14:31:22
【问题描述】:
首先,对不起我的英语不好。英语不是我的主要语言,我的写作能力太差了......
我已经搜索了一些关于这个问题的主题。我有一些类似的主题,但我无法获得适合我的解决方案。 [例如:error : binary '>>' : no operator found which takes a right-hand operand of type 'const char [1] And program crashes after taking first input,VS2013 C++ error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion)]
我只是发现问题是我不能在 istream 中使用类似“+”的东西,因为它不是变量,所以我不能使用它。但我想以如下形式输入我的类值: a + bj ,所以我可以使用 fstream 将其导出到 txt 文件。现在我不知道该怎么做。
我的代码:
#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;
class Complex
{
public:
Complex( double = 0.0, double = 0.0);
Complex(const Complex &);
Complex(const Complex *);
Complex operator+( const Complex & ) const;
Complex operator-( const Complex & ) const;
Complex operator*( const Complex & ) const;
Complex operator/( const Complex & ) const;
Complex operator=( const Complex & );
friend istream &operator>>(istream & in, const Complex & Com){
in >> Com.real >> " + " >> Com.imaginary >> "j\n"; //Error occur here
return in;
}
friend ostream &operator<<(ostream & out, const Complex & Com){
out << Com.real << " + " << Com.imaginary << "j\n";
return out;
}
void print() const;
private:
double real;
double imaginary;
};
【问题讨论】:
-
你想如何通过这种方法从
std::istream&读入Complex:friend istream &operator>>(istream & in, const Complex & Com)?此方法将 immutableComplex引用作为第二个参数。 -
您认为
in >> " + "和in >> j\n应该做什么?这没有任何意义。
标签: c++