0
我遇到了一個錯誤,我似乎無法弄清楚我需要編寫的最基本的程序,並最終適應更復雜的一段代碼。C++ IOStream操作符過載錯誤
Wordnum.cpp:10:67: error: ‘std::ostream& Wordnum::operator<<(std::ostream&, const Wordnum&)’ must take exactly one argument
Wordnum.cpp:14:61: error: ‘std::ostream& Wordnum::operator>>(std::istream&, Wordnum&)’ must take exactly one argument
這些是我的兩個錯誤,這裏是我的三個文件。
Main.cpp的
#include <cstdlib>
#include <iostream>
#include "Wordnum.h"
using namespace std;
int main(int argc, char** argv) {
float n1, n2;
char op;
while (cin >> n1 >> op >> n2) {
Wordnum a(n1), b(n2);
switch (op) {
case '+': cout << a + b << endl; break;
}
}
return 0;
}
Wordnum.h
#ifndef WORDNUM_H
#define WORDNUM_H
#include <iostream>
class Wordnum {
public:
Wordnum(int n); // CONSTRUCTOR
friend Wordnum operator+ (const Wordnum& n1, const Wordnum& n2)
{ return Wordnum(n1.value_ + n2.value_); }
friend Wordnum operator- (const Wordnum& n1, const Wordnum& n2)
{ return Wordnum(n1.value_ - n2.value_); }
friend Wordnum operator* (const Wordnum& n1, const Wordnum& n2)
{ return Wordnum(n1.value_ * n2.value_); }
friend Wordnum operator/ (const Wordnum& n1, const Wordnum& n2)
{ return Wordnum(n1.value_/n2.value_); }
friend std::ostream& operator<< (std::ostream&, const Wordnum& n);
friend std::istream& operator>> (std::istream&, Wordnum& n);
private:
int value_;
};
#endif /* WORDNUM_H */
Wordnum.cpp
#include "Wordnum.h"
#include <iostream>
Wordnum::Wordnum(int n) {
value_ = n;
}
std::ostream& Wordnum::operator<< (std::ostream&, const Wordnum& n) {
return 0;
}
std::ostream& Wordnum::operator>> (std::istream&, Wordnum& n) {
return 0;
}
編輯:並應返回參考流... – Naszta 2012-04-05 06:57:12
@Naszta我不好感謝 – keety 2012-04-05 07:01:06
感謝您的幫助,現在所有的工作。 – Edge 2012-04-05 07:26:05