#include"MyString.h"
#include<iostream>
MyString::MyString()//default constructor
{
length=0;
data=NULL;
cout<<"Default called by right none is called"<<endl;
system("pause");
}
MyString::MyString(char *source)//cstyle string parameter
{
int counter=0;
//we implement count without using getlen
for(int i=0;(source[i])!='\0';i++)//assume their char string is terminated by null
{
counter++;
}
length=counter;
cout<<"THE LENGTH of "<<source<<" is "<<counter<<endl;
system("pause");
data = new char[length];
}
void MyString::print(ostream a)//what to put in besides ostream
{
a<<data;
}
上面是我在執行文件C++簡單的cout的ostream
這是我的主文件
int main()
{
MyString s1("abcd");// constructor with cstyle style array
s1.print(cout);
system("pause");
return 0;
}
爲什麼不能這項工作? 即時得到這個錯誤
錯誤C2248: '的std :: basic_ios < _Elem,_Traits> :: basic_ios':不能訪問類中聲明私有成員 '的std :: basic_ios < _Elem,_Traits>'
萬元謝謝!錯誤固定!
不知道爲什麼不工作(不看詳細),但你可能要考慮你的類實現I/O以同樣的方式99.9999% C++編碼器呢。因此,我的意思是用'operator <<'而不是C++的一半編碼,C :-的一半 – paxdiablo 2012-02-12 04:23:39