Strange error! Compiler problem? Or code problem?!!!!!!!

Discussion in 'C++' started by Pedro Miguel Carvalho, Oct 25, 2004.

  1. Greetings.

    The following code does not compile and I can't see why! I'm using
    C++Builder v6.0 (build 10.160).

    <code>
    #include <vector>

    class test
    {
    bool Fixed( unsigned int I ) const;
    bool& Fixed( unsigned int I );

    private:

    std::vector<bool> pFixed;

    };

    bool test::Fixed( unsigned int I ) const
    {
    return pFixed;
    }

    bool& test::Fixed( unsigned int I )
    {
    return pFixed; // error: [C++ Error] test.cpp(21): E2357 Reference
    initialized with 'bool', needs lvalue of type 'bool'
    }
    </code>

    If the bool's are replace with int,float,double,a struct, a class, a enum it
    compiles and works correctly.
    The following code <code> pFixed = true; </code> also works so
    "pFixed" is a lvalue.

    Is seems to be a compiler error but how can this error go unnoticed?

    Thanks,
    Pedro Carvalho
    Pedro Miguel Carvalho, Oct 25, 2004
    #1
    1. Advertising

  2. The problem is in the code.

    vector< bool> has a different implementation than the generic vector<T>
    class.
    The operator[] returns a vector<bool>::reference type, which for vector<
    bool> is an inner class having defined a conversion operator bool() which
    returns a boolean value, not a boolean reference.

    In conclusion, the operator[] has as return type different than bool&, which
    can be (and is) automatically casted to bool (and not bool&).

    However, in the posted example the Fixed methods can be removed. Use direct
    access to pFixed member.

    Catalin

    "Pedro Miguel Carvalho" <*SPAM*BLOCK**> wrote in message
    news:417cf195$0$12624$...
    > Greetings.
    >
    > The following code does not compile and I can't see why! I'm using
    > C++Builder v6.0 (build 10.160).
    >
    > <code>
    > #include <vector>
    >
    > class test
    > {
    > bool Fixed( unsigned int I ) const;
    > bool& Fixed( unsigned int I );
    >
    > private:
    >
    > std::vector<bool> pFixed;
    >
    > };
    >
    > bool test::Fixed( unsigned int I ) const
    > {
    > return pFixed;
    > }
    >
    > bool& test::Fixed( unsigned int I )
    > {
    > return pFixed; // error: [C++ Error] test.cpp(21): E2357 Reference
    > initialized with 'bool', needs lvalue of type 'bool'
    > }
    > </code>
    >
    > If the bool's are replace with int,float,double,a struct, a class, a enum
    > it
    > compiles and works correctly.
    > The following code <code> pFixed = true; </code> also works so
    > "pFixed" is a lvalue.
    >
    > Is seems to be a compiler error but how can this error go unnoticed?
    >
    > Thanks,
    > Pedro Carvalho
    >
    >
    Catalin Pitis, Oct 25, 2004
    #2
    1. Advertising

  3. Ignore it!
    I'm having a strange day, no special cases allowed :).

    Pedro Carvalho

    "Pedro Miguel Carvalho" <*SPAM*BLOCK**> wrote in message
    news:417cf195$0$12624$...
    | Greetings.
    |
    | The following code does not compile and I can't see why! I'm using
    | C++Builder v6.0 (build 10.160).
    |
    | <code>
    | #include <vector>
    |
    | class test
    | {
    | bool Fixed( unsigned int I ) const;
    | bool& Fixed( unsigned int I );
    |
    | private:
    |
    | std::vector<bool> pFixed;
    |
    | };
    |
    | bool test::Fixed( unsigned int I ) const
    | {
    | return pFixed;
    | }
    |
    | bool& test::Fixed( unsigned int I )
    | {
    | return pFixed; // error: [C++ Error] test.cpp(21): E2357 Reference
    | initialized with 'bool', needs lvalue of type 'bool'
    | }
    | </code>
    |
    | If the bool's are replace with int,float,double,a struct, a class, a enum
    it
    | compiles and works correctly.
    | The following code <code> pFixed = true; </code> also works so
    | "pFixed" is a lvalue.
    |
    | Is seems to be a compiler error but how can this error go unnoticed?
    |
    | Thanks,
    | Pedro Carvalho
    Pedro Miguel Carvalho, Oct 25, 2004
    #3
    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. Yan
    Replies:
    0
    Views:
    1,132
  2. Jack Wright
    Replies:
    5
    Views:
    623
    Shiv Kumar
    Jan 19, 2004
  3. Kris Vanherck

    Strange compiler behavior after CS0006 error

    Kris Vanherck, Apr 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    449
    Ignacio Machin \( .NET/ C# MVP \)
    Apr 21, 2004
  4. Ram
    Replies:
    0
    Views:
    2,832
  5. Harvey Twyman
    Replies:
    8
    Views:
    562
    August Derleth
    Oct 25, 2003
Loading...

Share This Page