problem with manipulators in operator<<

Jun 21, 2013 at 5:49am
Hello all.

I write operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::ostream& operator<<(std::ostream &out, CDefect &defect) {
   std::streamsize   size  = 8;
   //std::resetiosflags(std::ios::showbase);
   //out.unsetf(std::ios::showbase);
   return out
      << "CDefect:"
      << "\tid= "       << std::setw(4) << std::right << defect.getId()
      << "\tidParent= " << std::setw(4) << std::right << defect.parent->getId()
      << "\tstatus= "   << defect.status
      << "\tr= "        << defect.r
      << "\tcenter: ("
      << std::fixed << std::setw(size) << std::setprecision(2) << defect.center.xyz.x << ", "
      << std::fixed << std::setw(size) << std::setprecision(2) << defect.center.xyz.y << ", "
      << std::fixed << std::setw(size) << std::setprecision(2) << defect.center.xyz.z << ")"
      << "\t" << defect.colorCB
      << "\tfileName: '" + defect.sFileName + "'"
      ;
}


It output information about the object into stream. And I log 3 object in cycle.

output:

CDefect: id=   54 idParent=   53 status= 0 r= 700 center: (-6918.26,  3463.00 , 44918.00)	RGB: {   0,   0,  54 }	fileName: 'C:\6424860467.jpg'
CDefect: id=   55 idParent=   52 status= 1 r= 2000.00	center: (-1330.00, -2206.15, -46925.00)	RGB: {   0,   0,  55 }	fileName: 'D:\Work\Weld\WinOGL\WinOGL\WinOGL\Data\fallout2.PNG'
CDefect: id=   56 idParent=   51 status= 2 r= 4000.00	center: (-14486.00, -2217.00, -15982.00)	RGB: {   0,   0,  56 }	fileName: ''


Why is the output of the first object does not fulfill all the manipulators? The problems I have highlighted in bold:

...r= 700 center: (-6918.26, 3463.00 , 44918.00)...

How to fix it?
Jun 21, 2013 at 7:48am
Why is the output of the first object does not fulfill all the manipulators?

It does. Why do you think it doesn't? (Or, to put the question another way, where do you see std::setprecision called before line 10?)
Last edited on Jun 21, 2013 at 7:52am
Jun 21, 2013 at 8:20am
thanks! helped.
Jun 21, 2013 at 10:48am
PS std::setw is the only one of the i/o manipulator that isn't "sticky". With the others (that set something) -- including std::setprecision, std::right, and std::fixed -- once you've set them they stay set, until you reset or set them again.

Does CDefect::r need to be a double (or float?) value? If the ID is always a whole number you could use an integral type instead.

Andy
Last edited on Jun 21, 2013 at 1:27pm
Topic archived. No new replies allowed.