刚刚学习C ++ - 一个问题 [英] Just learning C++ - a question
问题描述
我创建了一个类并用它来进一步超载ostream:
类鼓
{
...
朋友ostream&运营商LT;< (ostream&,drum const&);
}
ostream&运营商LT;< (ostream& out,drum const& od)
{
....
退出;
}
这很好用。现在我也意识到通过将类
引用作为const传递,被调用的子例程被阻止通过编译时检查将'/ b $ b'写入''鼓''。 />
但是只是为了咯咯笑,我通过
省略了''const''改变了声明和定义。我收到了很多编译错误,并且无法完全遵循他们的意思。
所以我的问题是,为什么引用鼓必须'' const''??
这是我的(gnu cygwin)编译器的怪癖还是......
谢谢百万
Wick
I have created a class and used it to further overload ostream:
class drum
{
...
friend ostream& operator<< ( ostream&, drum const& );
}
ostream& operator<< ( ostream& out, drum const& od )
{
....
return out;
}
and this works great. Now I also realize that by passing the class
reference as a const, the called subroutine is prevented from writing
to ''drum'' via compile time checking.
But just for giggles, I changed the declaration and definition by
omitting the ''const''. I received many compile errors and could not
quite follow what they meant.
So my question is, why does the reference to drum HAVE TO BE ''const'' ??
Is that a quirk of my (gnu cygwin) compiler or ...
Thanks a million
Wick
推荐答案
wickwire写道:
wickwire wrote:
所以我的问题是,为什么鼓的引用必须是常量?
这是我的(gnu cygwin)编译器的怪癖还是...
So my question is, why does the reference to drum HAVE TO BE ''const'' ??
Is that a quirk of my (gnu cygwin) compiler or ...
也许你正在尝试打印一个const鼓?没有调用代码就无法知道
。
Maybe you are attempting to print a const drum? No way of knowing
without calling code.
wickwire写道:
wickwire wrote:
我创建了一个类并用它来进一步超载ostream:
类鼓
{
.. 。
朋友ostream&运营商LT;< (ostream&,drum const&);
}
ostream&运营商LT;< (ostream& out,drum const& od)
{
....
退出;
}
这很好用。现在我也意识到通过将类
引用作为const传递,被调用的子例程被阻止通过编译时检查将'/ b $ b'写入''鼓''。 />
但是只是为了咯咯笑,我通过
省略了''const''改变了声明和定义。我收到了很多编译错误,并且无法完全遵循他们的意思。
所以我的问题是,为什么引用鼓必须'' const''
??这是我的(gnu cygwin)编译器的怪癖还是...
I have created a class and used it to further overload ostream:
class drum
{
...
friend ostream& operator<< ( ostream&, drum const& );
}
ostream& operator<< ( ostream& out, drum const& od )
{
....
return out;
}
and this works great. Now I also realize that by passing the class
reference as a const, the called subroutine is prevented from writing
to ''drum'' via compile time checking.
But just for giggles, I changed the declaration and definition by
omitting the ''const''. I received many compile errors and could not
quite follow what they meant.
So my question is, why does the reference to drum HAVE TO BE ''const''
?? Is that a quirk of my (gnu cygwin) compiler or ...
它不一定是const。您收到的错误消息最多是因为您使用运算符的方式 - 您想要的对象
输出_was_实际上是一个const对象。但是正如你所看到的,我在这里猜测是b $ b:你没有按照常见问题5.8的建议。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
It doesn''t have to be const. The error messages you received were most
like due to the way you used your operator -- the object which you wanted
to output _was_ in fact a const object, probably. But as you see, I am
guessing here -- you didn''t follow the recommendations of FAQ 5.8.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Victor Bazarov写道:
Victor Bazarov wrote:
它没有是const。您收到的错误消息最多是因为您使用运算符的方式 - 您想要的对象
输出_was_实际上是一个const对象。但是如您所见,我在这里猜测 - 你没有按照FAQ 5.8的建议。
It doesn''t have to be const. The error messages you received were most
like due to the way you used your operator -- the object which you wanted
to output _was_ in fact a const object, probably. But as you see, I am
guessing here -- you didn''t follow the recommendations of FAQ 5.8.
好的,这是代码和错误消息:
************ ************************************** ************ ***********
#include< iostream>
#include< iomanip>
>
使用命名空间std;
class Base
{
public:
~Base(){}
Base(int val,int num){}
朋友ostream&运营商LT;< (ostream&,Base&);
};
ostream&运营商LT;< (ostream& os,Base& obj)
{
return os<< hi;
}
int main()
{
int u5 = 100;
cout<< setw(0)<<基数(u5,0)<<结束;
返回0;
}
**************** ********************************** **************** ***** $
g ++ o.cpp -oo
o.cpp:在函数`int main()''中:
o.cpp:22:错误:''运算符<<''在''std :: operator<< [with
_CharT = char,_Traits =
std :: char_traits< char>](((std :: basic_ostream< char,
std :: char_traits< char&)(& std :: cout)),std :: setw(0))<<基数(u5,0)''
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c ++ / bits / ostream.tcc:63:
注意:候选人是:std :: basic_ostream< _CharT,_Traits>&
std :: basic_ostream< _CharT,
_Traits> :: operator<<(std :: basic_ostream< _CharT,
_Traits>&(*)(std :: basic_ostream< _CharT,_Traits>&))[with _CharT =
char,_Traits = std :: char_traits< char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ ostream.tcc:74:
注意:std :: basic_ostream< _CharT,_Traits>& std :: basic_ostream< _CharT,
_Traits> :: operator<<(std :: basic_ios< _CharT,
_Traits>&(*)(std :: basic_ios< _CharT,_Traits>&))[with _CharT = char,
_Traits = std :: char_traits< char>]
谢谢
Wick
OK, here is the code and error message:
************************************************** ***********************
#include <iostream>
#include <iomanip>
using namespace std;
class Base
{
public:
~Base(){}
Base(int val, int num) { }
friend ostream& operator<< ( ostream&, Base& );
};
ostream& operator<< ( ostream &os, Base& obj )
{
return os << "hi";
}
int main()
{
int u5 = 100;
cout << setw(0) << Base(u5,0) << endl;
return 0;
}
************************************************** ***********************
g++ o.cpp -o o
o.cpp: In function `int main()'':
o.cpp:22: error: no match for ''operator<<'' in ''std::operator<< [with
_CharT = char, _Traits =
std::char_traits<char>](((std::basic_ostream<char,
std::char_traits<char&)(&std::cout)), std::setw(0)) << Base(u5, 0)''
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:63:
note: candidates are: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT =
char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:74:
note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
<<< The list of candidates is quite long >>>
Thanks
Wick
>
这篇关于刚刚学习C ++ - 一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!