I'm new to C++ and I'm trying to pass a collection of a nonvariable amount strings to a function that exists in separate class file in an easy to read manner as such:
//main in Caller.cpp
int main()
{
string details[] = {"Name","Height","Weight"};
/* vector<string> detailsV = {"Name","Height","Weight"};
* Would like to use a vector but can't do this because vector cannot be
* initialized to = {...} in C++
*/
Person p = Person();
p.inspectDetails(details);
}
//Person class in Person.cpp
void inspectDetails(string details [])
{
int sz = sizeof(details); // this will result in details = "Name" only
}
I've tried:
//Person class in Person.cpp
<template size_t N>
void inspectDetails(string (&details)[N])
{
int sz = sizeof(details);
}
However, I don't now how to let the main in the Caller class know about the <template size_t N>
which might allow me to use an array of a non-explicit amount. I seem to get an error of "no suitable conversion of std:string[3] to std:string" when trying to call inpectDetails
this way.
What is the best way to pass a collection of strings of a non-explicit amount to a function outside of the Caller class whilst maintaining the ability to hardcode the collection's contents like so Collection c = {"...", "...", "..."}
in C++?
Is there an easier way to pass the full collection of strings to a function with a pointer to a vector or something of that sort?