S
SpOiLeR
Why doesn't following code compile? Problem line is commented in code.
---------------------- Cut here-------------------------------
#include <iostream>
#include <list>
#include <string>
using namespace std;
namespace namsp1 {
typedef list<string> TextBlock;
ostream& operator<< (ostream& dest, const TextBlock& tb)
{
if (dest) {
TextBlock::const_iterator i, iend;
i = tb.begin();
iend = tb.end();
for (; i != iend; i++)
dest << *i << endl;
}
return dest;
}
namespace namsp2 {
class myclass
{
public:
myclass ();
typedef struct {
TextBlock itsText;
int itsNumber;
} mc_struct;
friend std:stream& operator<< (std:stream& dest, const mc_struct&
mc);
private:
mc_struct itsStruct;
};
myclass::myclass () :
itsStruct()
{
itsStruct.itsNumber = 0;
}
ostream& operator<< (ostream& dest, const myclass::mc_struct& mc)
{
if (dest) {
// Next one doesn't work
dest << mc.itsText;
// error returned is:
// binary '<<' : no operator found which takes a right-hand operand
// of type 'const namsp1::TextBlock'
// (or there is no acceptable conversion)
// Next line solves the problem but why the previous doesn't work?
// namsp1:perator<< (dest, mc.itsText);
dest << mc.itsNumber;
}
return dest;
}
} // namespace namsp2
} // namespace namsp1
---------------------- Cut here-------------------------------
TIA
---------------------- Cut here-------------------------------
#include <iostream>
#include <list>
#include <string>
using namespace std;
namespace namsp1 {
typedef list<string> TextBlock;
ostream& operator<< (ostream& dest, const TextBlock& tb)
{
if (dest) {
TextBlock::const_iterator i, iend;
i = tb.begin();
iend = tb.end();
for (; i != iend; i++)
dest << *i << endl;
}
return dest;
}
namespace namsp2 {
class myclass
{
public:
myclass ();
typedef struct {
TextBlock itsText;
int itsNumber;
} mc_struct;
friend std:stream& operator<< (std:stream& dest, const mc_struct&
mc);
private:
mc_struct itsStruct;
};
myclass::myclass () :
itsStruct()
{
itsStruct.itsNumber = 0;
}
ostream& operator<< (ostream& dest, const myclass::mc_struct& mc)
{
if (dest) {
// Next one doesn't work
dest << mc.itsText;
// error returned is:
// binary '<<' : no operator found which takes a right-hand operand
// of type 'const namsp1::TextBlock'
// (or there is no acceptable conversion)
// Next line solves the problem but why the previous doesn't work?
// namsp1:perator<< (dest, mc.itsText);
dest << mc.itsNumber;
}
return dest;
}
} // namespace namsp2
} // namespace namsp1
---------------------- Cut here-------------------------------
TIA