Ask a questionAsk a question
 

Questionauto for class members

  • Sunday, February 22, 2009 10:00 AMMarius BancilaMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Here is a piece of code:

    class foo
    {
       auto data;
       const auto MAX;
       auto msg;

    public:
       foo(int val, string m) :
          data(val),
          msg(m),
          MAX("42")
       {}
    };

    The compiler shows 2 errors:

    cpp_auto.cpp(23) : error C2440: 'initializing' : cannot convert from 'std::string' to 'auto'
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    cpp_auto.cpp(23) : error C2439: 'foo::msg' : member could not be initialized
            c:\users\administrator\documents\visual studio 10\projects\cpp_auto\cpp_auto.cpp(16) : see declaration of 'foo::msg'

    It looks like it treats auto as int (default type when none specified?), but then why does initializing the MAX constant with a string works. As far as I know, auto can only be used locally. So I think there are some problems here with the compiler.



    Microsoft MVP VC++ | www.mariusbancila.ro | www.mariusbancila.ro/blog

All Replies

  • Monday, May 11, 2009 3:55 AMJonathan Caves - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Marius: this is a bug - the code should not compile but the error message is not the best. We'll fix this for the next release.

    Jonathan Caves 


    Jonathan Caves - Visual C++ Compiler Team