Tra*_*tto 2 c++ compiler-errors operator-overloading
我试图重载我的Currency类的<<运算符,但我得到此编译器错误: C2143: syntax error : missing ';' before '&'
在我的.h文件中,我有:
friend ostream &operator << (ostream &, const Currency&);
Run Code Online (Sandbox Code Playgroud)
在我的Currency.cpp文件中,我有:
ostream &operator << (ostream &stream, const Currency &obj){
stream<<"$"<<obj.dollars<<"."<<obj.cents;
return stream;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都运行良好,但是一旦我把它放进去,就会窒息:
我的.h文件顶部有以下内容:
#ifndef CURRENCY_H
#define CURRENCY_H
#include<iostream>
#include<string>
#include<ostream>
#include<sstream>
class Currency; //forward delcaration
//Function prototypes for overloaded stream operators
ostream &operator << (ostream &, const Currency &);
Run Code Online (Sandbox Code Playgroud)
我不知道我做错了什么.帮助会很棒.谢谢
ostream声明在namespace std你std::之前,你缺少标识符:
std::ostream &operator << (std::ostream &, const Currency &);
Run Code Online (Sandbox Code Playgroud)
如果你想避免std::在头文件之后你可以把using namespace语句:
...
#include<ostream>
using namespace std; // this is not desirable though in real world programming
ostream &operator << (ostream &, const Currency &);
Run Code Online (Sandbox Code Playgroud)
编辑: using namespace <>建议不要在文件顶部进行实际编程.我把这部分仅仅用于FYI.
| 归档时间: |
|
| 查看次数: |
760 次 |
| 最近记录: |