Pass a vector of strings to a function without declaring vector

I have tried to do what the title says but always getting some errors and a warning:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
#include <string>
#include <iostream>

using namespace std;

void Print(const vector<string>& s) {
	for (int i = 0; i < s.size(); i++) cout << s[i] << '\n';
        // warning: < signed/unsigned mismatch
}

int main () {
	Print(vector<string> {"Play", "Option", "Exit"});
        // error:
        // syntax error: missing ')' before '{'
        // syntax error: missing ';' before '{'
        // syntax error: missing ';' before '}'
        // syntax error: ')'
        // std::vector<_Ty>: illegal use of this type as an expression

	cin.get();
	return 0;
}
Last edited on
Have you tried deleting "vector<string>" from line 13? You will be passing a std::initializer_list<std::string> which creates a temporary vector whose lifetime is extended while Print() executes.

Also, the warning is because s.size() returns std::vector<string>::size_type, which is an unsigned integral type. Change the type of I to unsigned instead (or std::size_t).
Ok. Thanks.
Topic archived. No new replies allowed.