Vector Of Structs Problem

Hello, I'm having problems passing a vector of structs to a function. What I'm trying to do is create a program that implements of a vector of structs which hold data about an object. Thanks in advance if you can provide any help.


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
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdio>
#include <vector>
#include <algorithm> 
#include <fstream>

        //Struct to hold the card values
	struct Car
	{
		std::string CarName;
		int CarYear;
		int CarNum;
	};
   
        //Vector prototype 
	std::vector<Car> Initialize(std::vector<Car> &MVehicle);
	
void main()
{
	
//Instance of struct
std::vector<Car> Vehicle;

//Pass the vector of struct to the function
Vehicle = Initialize(Vehicle);

//Function call
Initialize(Vehicle);
				
	system ("PAUSE");
}

std::vector<Car> Initialize(std::vector<Car> &MVehicle)
{

	return MVehicle;
}
Which problem are you having?
Sorry about that. The problem i'm having is that I can't access the struct once i'm inside the function. For instance, I can't do MVehicle.CarName and enter a value for that struct. Instead, it gives me a lists of options for a vector only. So it looks like i'm only passing a vector instead of a struct.
You are passing the vector: std::vector<Car> Initialize(std::vector<Car> &MVehicle)
Yeah, but I need to pass the vector with structs inside of it. I can't figure out what the syntax would be in order to do that.
You are passing a vector of struct objects, each element of the vector would be an object of the struct ( the vector you are passing is empty )
What are you trying to do in that function?
Instead of MVehicle.CarName try MVehilcle.at(0).CarName.
If the argument passed is an empty vector, it won't work
Topic archived. No new replies allowed.