strange error message

Discussion in 'C++' started by aaragon, Apr 1, 2008.

  1. aaragon

    aaragon Guest

    Hi everyone,

    Can someone explain me the weird error message that I got from this
    simple code?

    struct VTKP {
    std::eek:stream& os_;
    VTKP(std::eek:stream& os) : os_(os) {}
    void operator()(int i) {
    os_<<i<<endl;
    }
    };

    int main(int argc, char *argv[]) {

    std::eek:fstream 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
    ....

    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::eek:fstream 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
    aaragon, Apr 1, 2008
    #1
    1. Advertising

  2. aaragon

    Jim Langston Guest

    aaragon wrote:
    > Hi everyone,
    >
    > Can someone explain me the weird error message that I got from this
    > simple code?
    >
    > struct VTKP {
    > std::eek:stream& os_;
    > VTKP(std::eek:stream& os) : os_(os) {}
    > void operator()(int i) {
    > os_<<i<<endl;
    > }
    > };
    >
    > int main(int argc, char *argv[]) {
    >
    > std::eek:fstream 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::eek:stream 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::eek:fstream. 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::eek:fstream 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
    Jim Langston, Apr 1, 2008
    #2
    1. Advertising

  3. aaragon

    James Kanze Guest

    On Apr 1, 7:02 am, "Jim Langston" <> wrote:
    > aaragon wrote:
    > > Can someone explain me the weird error message that I got
    > > from this simple code?


    > > struct VTKP {
    > > std::eek:stream& os_;
    > > VTKP(std::eek:stream& os) : os_(os) {}
    > > void operator()(int i) {
    > > os_<<i<<endl;
    > > }
    > > };


    > > int main(int argc, char *argv[]) {


    > > std::eek:fstream 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::eek:stream
    > 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::eek:fstream.


    FWIW: the standard requires a compiler to look at it this was.
    If something can be considered a declaration, it is a
    declaration.

    I could imagine a compiler warning about a declaration with a
    useless outer set of parentheses, and a really good compiler, in
    such cases, would look at the conflicting declarations, and
    condition its error message depending on whether one or both
    could be interpreted as an expression. But as written the
    statement in question defines a variable dout, there is already
    a variable with the same name in scope, and the compiler is
    required to issue an error message.

    > 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.


    Either putting just the type name, or the entire expression, in
    parentheses is sufficient to prevent the text being interpreted
    as a declaration---declarations can't be in parentheses, and
    parentheses can't occur in the declaration specifier (the first
    part of the declaration, which contains the type name).

    Whether such a solution is clearer than an explicit temporary,
    of course, is another question. Something like (VTKP)(dout)(10)
    could confuse some people.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 1, 2008
    #3
  4. aaragon

    aaragon Guest

    On Apr 1, 4:51 am, James Kanze <> wrote:
    > On Apr 1, 7:02 am, "Jim Langston" <> wrote:
    >
    >
    >
    > > aaragon wrote:
    > > > Can someone explain me the weird error message that I got
    > > > from this simple code?
    > > > struct VTKP {
    > > > std::eek:stream& os_;
    > > > VTKP(std::eek:stream& os) : os_(os) {}
    > > > void operator()(int i) {
    > > > os_<<i<<endl;
    > > > }
    > > > };
    > > > int main(int argc, char *argv[]) {
    > > > std::eek:fstream 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::eek:stream
    > > 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::eek:fstream.

    >
    > FWIW: the standard requires a compiler to look at it this was.
    > If something can be considered a declaration, it is a
    > declaration.
    >
    > I could imagine a compiler warning about a declaration with a
    > useless outer set of parentheses, and a really good compiler, in
    > such cases, would look at the conflicting declarations, and
    > condition its error message depending on whether one or both
    > could be interpreted as an expression. But as written the
    > statement in question defines a variable dout, there is already
    > a variable with the same name in scope, and the compiler is
    > required to issue an error message.
    >
    > > 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.

    >
    > Either putting just the type name, or the entire expression, in
    > parentheses is sufficient to prevent the text being interpreted
    > as a declaration---declarations can't be in parentheses, and
    > parentheses can't occur in the declaration specifier (the first
    > part of the declaration, which contains the type name).
    >
    > Whether such a solution is clearer than an explicit temporary,
    > of course, is another question. Something like (VTKP)(dout)(10)
    > could confuse some people.
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    Thanks for the info...
    aaragon, Apr 2, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Wehofer

    Strange error message at client

    Robert Wehofer, Nov 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    429
    Chris Jackson
    Nov 17, 2003
  2. jack
    Replies:
    0
    Views:
    691
  3. Replies:
    3
    Views:
    384
    Roedy Green
    Nov 26, 2005
  4. Replies:
    0
    Views:
    317
  5. Rogério Brito
    Replies:
    1
    Views:
    639
    Sean DiZazzo
    Sep 9, 2009
Loading...

Share This Page