C++ Simple cout ostream
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])!=' ';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)
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- ostream过载时的缓冲区冲洗
- std::cout.imbue()多重调用
- 如何在C++中编写 ostream 的向量,它接收所有不同的输出流,如 cout、ostringstream 和 ofs
- ostream:在 cout 或文件上输出的类
- 如何创建一个新类来继承 ostream 并将其用作 cout 但带有锁定
- 无法使用Ostream打印,但可以使用COUT打印
- COUT不使用Ostream标头文件
- 为什么我可以在 lambda 中使用 ostream cout,尽管它没有被捕获?
- 无法将 std::cout 传递给 ostream&constructor
- cout/cerr包装ostream只需最少的努力
- 在C++重载cout ostream操作员
- C++ Simple cout ostream
- 输出到stdout和带有cout、cerr、clog甚至用户定义的ostream的文件
- C++11 std::shared_ptr<std::ostream> from std::cout
- 二进制表达式的操作数无效("std::__1::basic_ostream<char>"和"ostream"(又名"basic_ostream")) std::cout<<&l
- 操作符重载- cout和cin——ostream函数,不能被引用——它是一个已删除的函数
- Std::cout, ostream和其他类型的获取输出流
- 如何使用void printMatrix( ostream& os = cout ) const?
- 创建ostream到文件或cout的正确方法