vector by reference

closed account (yh54izwU)
I'm probably doing this wrong, I don't typically use vectors and I am starting to jump on the band wagon but I have no idea how to pass them by reference this is what I tried doing but I am getting a illegal use of this type of expression error. Any ideas would be great as I am lost thanks



void ques3(int *narow,int *right,int *wrong){
srand(time(0));
int pullnames=5;
vector<string> ques3namelist;
vector<string> ques3namespulled;
quesnames(*narow,vector<string> &ques3namelist,vector<string> &ques3namespulled);




}




//I also tried quesnames(*narow,&ques3namelist, &ques3namespulled);
Last edited on
If you want to pass it as a reference, just give the name like you are passing by value normally.
closed account (yh54izwU)
Do you mean just &ques3namelist? Because for some reason that wasn't working either
No, just the name of the variable, like you are passing by value.
closed account (yh54izwU)
That doesn't seem to work because when I try to access an element it doesn't print anything



void ques3(int *narow,int *right,int *wrong){
srand(time(0));
vector<string> q3namespulled(*narow);
int rando=14;
int chk=0;
quesnames(*narow, q3namespulled);

cout<<q3namespulled[1];

}

is what i tried I hate to ask do I need to ask the element in a different way?
What does quesnames() do?
closed account (yh54izwU)
creates a list of random names
using


string names[] = {"Tina","Aaron","Alan","Christi","Edgar","Marie","Sophia","Dale","Frank","Bruce","Marilyn","Joe","Alice","Natalia","Paul", "Katherine", "Simon", "Nick","Chris","Josh","Jeremy", "Nisa", "Marlon","Will","Eliza","Kat","Raj","Howard","Amy","Penny"};

vector<string> nameslist(names, names+29);

pullednames.resize(nameslong);



//I use a for loop with a while loop nested inside to pull the random names
Im pretty sure Arrays and Vectors are passed by reference normally. I know that Arrays are just Pointers to char, so a vector couldnt be *too* different. Correct me if im wrong
Vectors are actually different, and are passed by value unless you specify otherwise.

@TC: What does the function definition look like? Do you modify the vector reference you are taking?
closed account (yh54izwU)
Wow never mind I made a STUPID mistake but thank you for your help
Topic archived. No new replies allowed.