VC++ 6.0下,重载<<运算符遇到的问题
时间:2011-11-28
来源:互联网
C/C++ code
//: IOSOP.CPP -- Iostream operator overloading // Example of non-member overload operators #include <iostream> #include <strstrea.h> #include <string.h> class intarray { enum { sz = 4 }; int i[sz]; public: intarray() { memset( i, 0, sz*sizeof(*i) ); } int& operator[] (int x) { return i[x]; } friend ostream& operator << ( ostream& os, const intarray& ia ); friend istream& operator >> ( ostream& is, intarray& ia ); }; ostream& operator << ( ostream os, const intarray& ia ){ for( int j = 0; j < ia.sz; j++ ){ os << ia.i[j]; if( j != ia.sz - 1 ) os << endl } os << endl; return os; } istream& operator>>( istream& is, intarray& ia ) { for( int j = 0; j< ia.sz; j++ ) is >> ia.i[j]; return is; } int main(void) { istrstream input("47 34 56 92 103"); intarray I; input >> I; I[4] = -1; cout << I; return 0; }
错误如下:
error C2248: 'sz' : cannot access private enumerator declared in class 'intarray'
求解答。。
作者: gengzhikui1111 发布时间: 2011-11-28
作者: czqfrms 发布时间: 2011-11-28
单看提示是访问类的私有变量了。
可我设置友元了啊
作者: gengzhikui1111 发布时间: 2011-11-28
#include <iostream> #include <sstream> using namespace std; class intarray { public: const static int sz = 4; intarray() { memset( i, 0, sz*sizeof(*i) ); } int& operator[] (int x) { return i[x]; } friend ostream& operator << ( ostream& os, const intarray& ia ); friend istream& operator >> ( ostream& is, intarray& ia ); int i[sz]; }; ostream& operator << ( ostream &os, const intarray& ia ){ for( int j = 0; j < ia.sz; j++ ){ os << ia.i[j]; if( j != ia.sz - 1 ) os << endl; } os << endl; return os; } istream& operator>>( istream& is, intarray& ia ) { for( int j = 0; j< ia.sz; j++ ) is >> ia.i[j]; return is; } int main(void) { istringstream input("47 34 56 92 103"); intarray I; input >> I; I[4] = -1; cout << I; return 0; }
作者: mougaidong 发布时间: 2011-11-28
另外,我用enum表示常量sz,有什么错误吗?
作者: gengzhikui1111 发布时间: 2011-11-28
换个编译器编译一下。
作者: taodm 发布时间: 2011-11-28
作者: lthyxy 发布时间: 2011-11-28
第一个形参类型错误了
还有你怎么不添加using namespace std;
作者: liux0614 发布时间: 2011-11-28
//: IOSOP.CPP -- Iostream operator overloading // Example of non-member overload operators #include <iostream.h> #include <strstrea.h> #include <string.h> class intarray { enum { sz = 4 }; int i[sz]; public: intarray() { memset( i, 0, sz*sizeof(*i) ); } int& operator[] (int x) { return i[x]; } friend ostream& operator<<( ostream& os, const intarray& ia ); friend istream& operator>>( istream& is, intarray& ia ); }; ostream& operator<<( ostream &os, const intarray& ia ){ for( int j = 0; j < ia.sz; j++ ){ os << ia.i[j]; if( j != ia.sz - 1 ) os << endl; } os << endl; return os; } istream& operator>>( istream& is, intarray& ia ) { for( int j = 0; j< ia.sz; j++ ) is >> ia.i[j]; return is; } int main(void) { istrstream input("47 34 56 92 103"); intarray I; input >> I; I[4] = -1; cout << I; return 0; }
作者: gengzhikui1111 发布时间: 2011-11-28
C/C++ code
//: IOSOP.CPP -- Iostream operator overloading
// Example of non-member overload operators
#include <iostream.h>
#include <strstrea.h>
#include <string.h>
class intarray {
enum { sz……
这样就行了。。嘿嘿。。初学者犯的拼写错误
作者: gengzhikui1111 发布时间: 2011-11-28
C/C++ code
#include <iostream>
#include <sstream>
using namespace std;
class intarray {
public:
const static int sz = 4;
intarray() {
memset( i, 0, sz*sizeof(*i) );
……
你的const static int sz = 4;
是什么意思啊?
作者: gengzhikui1111 发布时间: 2011-11-28
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28