Re: strange error message
aaragon wrote:
Hi everyone,
Can someone explain me the weird error message that I got from this
simple code?
struct VTKP {
std::ostream& os_;
VTKP(std::ostream& os) : os_(os) {}
void operator()(int i) {
os_<<i<<endl;
}
};
int main(int argc, char *argv[]) {
std::ofstream dout;
dout.open("domain.vtp");
dout<<"<?xml version=\"1.0\"?>\n";
dout<<"<VTKFile type=\"PolyData\" version=\"0.1\" byte_order=
\"LittleEndian\">\n";
dout<<"<PolyData>\n";
dout<<"<Piece NumberOfPoints=\"";
VTKP(dout)(10); // <<<<<<-------------------------------- error
The compiler is getting confused as to what you are actually doing there.
We can tell that you are trying to create a temporary VTKP object
initialized with the std::ostream reference of dout and then call the
operator () on the temp object.
Now, how is the compiler looking at it? Most likely like:
VTKP dout(10);
as the ()'s can be discarded, creating a VTKP named dout and passing 10 for
the initializer. Hence the error message, you can't create dout as a VTKP
becaue it's already a std::ofstream. Easiest way to fix it it to not make
it a temporary.
VTKP Temp(dout);
Temp(10);
You may find some other syntax that may work without having to name the
temorary but I'm not familiar with them and it would probably tend to
obfuscate your code and intentions.
...
main.cxx: In function 'int main(int, char**)':
main.cxx:94: error: conflicting declaration 'VTKP dout'
main.cxx:88: error: 'dout' has a previous declaration as
'std::ofstream dout'
make: *** [main.o] Error 1
and then I change that problematic line with
(VTKP(dout)(10));
and everything goes fine. I'm using GCC v4.3.
Thank you all,
aa
--
Jim Langston
tazmaster@rocketmail.com