[英]C++ Simple cout ostream
#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;
}
以上是在我的實現文件中
這是在我的主文件中
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>中聲明的私有成員
萬分感謝! 錯誤已修正!!
您不能復制std::cout
, std::cin
, std::cerr
或任何其他從std::ios_base
派生的對象,因為該對象的復制構造函數是私有的...您必須傳遞所有流對象通過引用從ios_base
派生,以防止復制構造函數的調用。 因此,您的功能簽名:
void MyString::print(ostream a);
需要至少更改為
void MyString::print(ostream& a);
原因是print
調用試圖復制輸出流,這是不允許的。 您已更改函數以將參數作為參考:
void MyString::print(ostream &a)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.