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

The reason is that the call to print tries to copy the output stream, which is not allowed. You have change the function to take the argument as a reference:

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