C++ snprintf "cannot pass objects of non-POD type"
c++, printf
Solution
`snprintf` knows nothing about `std::string`. In this case, it expects null-terminated C strings, that is, pointers to `char` which are the beginning of a sequence of characters that ends in a null character. You can obtain the underlying null terminated string held by a `std::string` object via its `c_str()` method:
snprintf(command, 256, "tar -xvzf %s %s", destination.c_str(), source.c_str());
Problem
I am trying to write a program that has a config and key file. The config file reads what ever is inputted into the key file, parses and executes the key values as needed. I am getting an error: warning: cannot pass objects of non-POD type ‘struct std::string’ through ‘...’; call will abort at runtime. I am receiving the error on this line: ``` snprintf(command, 256, "tar -xvzf %s %s", destination, source); system(command); ``` More of the code to try and better explain: ``` std::string source = cfg.getValueOfKey<std::string>("source"); std::string destination = cfg.getValueOfKey<std::string>("destination"); int duration = cfg.getValueOfKey<int>("duration"); int count, placeHolder, placeHolderAdvanced; count = 1; char command[256]; snprintf(command, 256, "tar -xvzf %s %s", destination, source); system(command); //Creates folder 1. snprintf(command, 256, "mkdir %i", count); system(command); //Removes the last folder in the group. snprintf(command, 256, "rm -rf %i", duration); system(command); ``` Any suggestions on what I am doing wrong, or where I should be looking? Thank you!