How can I pass vector a an argument; getting an error

Hello, I built a vector of string elements.
Then I am trying to pass such vector as an argument on another function. However I get an error

main.cpp:47:32: error: could not convert ‘get_student’ from ‘std::string {aka std::basic_string}’ to ‘std::vector >’
retrieveStudent(get_student);


Can you kindly let me know what is wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
#include <vector>
#include <string>

using namespace std;

//This will build a vector with student lastname, firstname and ID
string buildStudent()
{
    std::vector<string> rawCollStudents;
    
    string first = "John";
    string last =  "Smith";
    string ID   =  "111";
    string my_student = first + last + ID;
    //Now I have vector with string content in it
    rawCollStudents.push_back(my_student);
    
    return my_student;
    
    
}
//Now that I built vector, retrieve content from vector and print it
void retrieveStudent(vector<string> student) //<=== Goal is to pass content of vector rawCollStudents as argument here. It gives me an error
{
    for (string elem : student)
    {
        cout << elem;
    }

    
}

int main()
{
    string get_student;
    get_student = buildStudent();
    retrieveStudent(get_student);

    return 0;
}

Last edited on
Then I am trying to pass such vector


No you're not.

What object are you trying to pass? get_student
Why kind of object is that? It's a string

How do I know? Because you made it a string: string get_student;

Is a string the same as a vector<string>? No.

Oops, sorry. Thank you very much!
Topic archived. No new replies allowed.