-3

New to low level coding, coming from php, how do you properly define a member default parameters with a vector ?

     A(int x = 0, std::string y, 
std::vector<std::string> z = {0{"ScreenName","ID"}, ... } 
    ){
            switch(x){
                case 0: CreateNewPlayer(z["ScreenName"], z["ID"]); break;
                default:
                /* log function*/
                break;
              }
        }
    }
  • The way you are using `vector` is more like [`std::map`](https://en.cppreference.com/w/cpp/container/map) or [`std::unordered_map`](https://en.cppreference.com/w/cpp/container/unordered_map). `vector` is more like an array and works only with integer indexing. – user4581301 Oct 10 '18 at 03:13
  • 1
    Recommendation: C++ is a really powerful and sometimes weird language. You can't drop into it blind even with a programming background. You definitely want to approach the language with a good set of reference materials. [This is a link to a free reference by the originator of the language](https://isocpp.org/tour), but it is a little out of date and terse as hell. You will probably be better off leaking a bit of money and [picking something from this list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Oct 10 '18 at 03:23

1 Answers1

0

First of all do you need a std::map? vector doesn't have an operator that takes a std::string.

So you can't write z["key"] with a vector(but you can with a map)

To use default parameter you can say:

 A(int x = 0, std::string y = "", 
std::vector<std::string> z = {"ScreenName","ID"}, ... )

If you are using map then std::map<std::string, int>{{"screenName", 0}}

Check this question for details How to set default parameter as class object in c++?

Footnote: You may also want to read about the details of default arguments

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++14) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.

bashrc
  • 4,725
  • 1
  • 22
  • 49