3
#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;
    }

the above is in my implementation file

This is in my main file

 int main()
 {
    MyString s1("abcd");// constructor with cstyle style array
    s1.print(cout);
    system("pause");
    return 0;
 }

Why cant this work? Im getting this error

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

Million Thanks! ERROR FIXED!!

4

2 回答 2

3

您不能复制std::coutstd::cinstd::cerr或任何其他派生std::ios_base自的对象,因为该对象的复制构造函数是私有的……您必须传递通过引用派生的所有流对象ios_base,以防止调用复制构造函数。因此您的函数签名:

void MyString::print(ostream a);

至少需要更改为

void MyString::print(ostream& a);
于 2012-02-12T04:27:54.063 回答
2

原因是调用print试图复制输出流,这是不允许的。您已更改函数以将参数作为参考:

void MyString::print(ostream &a)
于 2012-02-12T04:29:25.303 回答