这是我之前发布的问题的pt2 如果它在我编辑后会被回答,因为它是 已经考虑过回答了#39;我想。
好的,我现在正在尝试输出a + bi:
std::ostream& operator<< (std::ostream& out, complex const& c) {
return out << c.getReal() << "+" << c.getImag() << "i";
}
并输入:
std::istream& operator>> (std::istream& in, complex& c) {
double h, j;
if (in >> h >> "+" >> j >> "i") {
c.set(h, j);
}
return in;
}
但是编译时出现以下错误:
这是我的complex.cpp文件(类复杂实现文件)的第181行,其中if (in >> h >> "+" >> j >> "i") {
位于上面的函数定义中:
binary '>>': no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion)
以下是我的complex.h文件中第45行(注意每个错误是单独的,总共7行),其中friend std::istream &operator>> (std::istream &in, complex& c);
原型所在。
'istream':is not a member of 'std'
syntax error missing ';' before '&'
'istream':'friend' not permitted on data declarations
missing type specifier-int assumed. Note:C++ does not support default-int
unexpected token(s) preceding';'
namespace "std" has no member "istream"
namespace "std" has no member "istream"
以下是我的complex.h文件的第46行
friend std::ostream &operator<<(std::ostream &out, complex c);
位于
'ostream': is not a member of 'std'
syntax error: missing ';' before '&'
'ostream':'friend' not permitted on data declarations
missing type specifier -int assumed.Note: C++ does not support default-int
unexpected token(s) preceding ';'
namespace "std" has no member "ostream"
namespace "std" has no member "ostream"
我注意到两者都是同一类型的错误。注意我有
#include<iostream>
using namespace std;
在complex.cpp文件和main.cpp文件
上答案 0 :(得分:3)
您正尝试在
中输入只读字符串文字if (in >> h >> "+" >> j >> "i")
哪个不行。您需要做的是创建一个变量来存储输入的文本内容。由于不需要内容,我们可以在完成后将其丢弃。这将给你一些
的内容std::istream& operator>> (std::istream& in, complex& c) {
double h, j;
char eater;
if (in >> h >> eater >> j >> eater) { // eater now consumes the + and i
c.set(h, j);
}
return in;
}
至于头文件中的错误,您需要在头文件中包含#include <iostream>
,以便编译器知道istream
和ostream
是什么。