Search code examples
c++c++11auto

can "auto function()" have several types of return inside the function body?


I am having problem with the code below,it generate errors and I believe is because apparently "auto" can't handle several types of return based on conditions. Is that the case or something else is wrong with my code? Also if what I am trying to do is not possible this way, it is possible in any other way?

auto Game_Manager::getMember(string s)
{
    if (s == "rows")return rows; // return unsigned
    else if (s == "columns")return columns; // return unsigned
    else if (s == "p1")return p1; //return string
    else if (s == "p2")return p2; //return string
    else cout << "\n\nERROR!!! Invalid argument for getMember()\n\n" << endl;
    return 1;
}

Solution

  • auto means "work out the type for me", not "accept any type". There is no (builtin) type that this function can be declared as, therefore auto is not valid