[英]cannot print using ostream but can by using cout?
Let this be the example code:让这是示例代码:
object o1(//parameters);
object o2(//parameters);
object o3(//parameters);
object *v[3];
using std::cout; //video output
ofstream of; //save on file
//let's suppose
v[0]=&o1;
v[1]=&o2;
v[2]=&o3;
for (int i=0;i<3;i++) {
v[i]->view(cout);
v[i]->save(of);
}
view function is just a video print function of parameters of the class, while save function saves parameters of the class on a file. view函数只是类参数的视频打印函数,而save函数将类的参数保存在文件中。 The question is, if i declare std::ostream;
问题是,如果我声明std::ostream;
and ostream os;
和ostream os;
why can't I use v[i]->view(os)
to have video output?为什么我不能使用v[i]->view(os)
来输出视频? If I use v[i]->view(os)
it says:如果我使用v[i]->view(os)
它说:
'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]' is protected
And while that's true for video output, for the saving function it is not, it works, just as intended in the code.虽然这对于视频输出来说是正确的,但对于保存功能来说却不是,它可以正常工作,就像代码中的预期一样。 Can someone explain?有人可以解释一下吗? sorry if i made it long对不起,如果我做得很长
1) If the view
function is defined as: 1) 如果view
函数定义为:
void view(std::ostream output, std::string text) // (1)
{
output << text;
}
And used:并使用:
view(std::cout, "Hello, World!"); // (2)
Then an error message is given by the compiler:然后编译器给出错误信息:
In MSVC:在 MSVC 中:
error C2280: 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<char,std::char_traits<char>> &)': attempting to reference a deleted function
GCC:海湾合作委员会:
error: use of deleted function 'std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]'
Clang:铛:
error: call to deleted constructor of 'std::ostream' (aka 'basic_ostream<char>')
2) For the declaration 2) 对于申报
std::ostream os;
The following error message is displayed:显示以下错误消息:
MSVC: MSVC:
error C2512: 'std::basic_ostream<char,std::char_traits<char>>': no appropriate default constructor available
GCC:海湾合作委员会:
error: 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]' is protected within this context
Clang:铛:
error: calling a protected constructor of class 'std::basic_ostream<char>'
This is all according to the specification of std::basic_ostream这都是根据std::basic_ostream的规范
There is no definition for a default constructor - so a variable of the type std::ostream
cannot be created, without specific constructor parameters.没有默认构造函数的定义 - 因此,如果没有特定的构造函数参数,则无法创建std::ostream
类型的变量。
And as C++ Reference says about the std::basic_ostream copy constructor :正如 C++ 参考所说的std::basic_ostream 复制构造函数:
The copy constructor is protected, and is deleted.复制构造函数受到保护,并被删除。 Output streams are not copyable.输出流不可复制。
1) So the problem is that in (2)
the parameter std::cout
is been passed to a function which is defined in (1)
to copy the std::ostream
to the variable output
. 1)所以问题是在(2)
中参数std::cout
被传递给一个在(1)
定义的函数,以将std::ostream
复制到变量output
。
But the definition of the class says that the copy constructor cannot be used, so the compiler gives an error message.但是类的定义说不能使用复制构造函数,所以编译器给出了错误信息。
2) In the case of creating the variable os
- it is not giving any constructor parameters, there is no default constructor, so the compiler gives an error message. 2) 在创建变量os
的情况下 - 它没有给出任何构造函数参数,没有默认构造函数,因此编译器给出了错误消息。
1) In the declaration of the function change the definition to take a reference ( &
) of std::ostream
as a parameter: 1) 在函数的声明中更改定义以将std::ostream
的引用 ( &
) 作为参数:
void view(std::ostream& output, std::string text) // (1)
This allows it to use the original object instead of making a copy (which the copy is not allowed).这允许它使用原始对象而不是制作副本(不允许复制)。
2) If a variable is required, then also a reference should be used; 2) 如果需要一个变量,那么也应该使用一个引用;
std::ostream& out = std::cout;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.