Passing a vector of structs to a function

Problem passing a vector of structs to a function

Page 1 of 1

4 Replies - 3928 Views - Last Post: 05 December 2009 - 10:13 AM Rate Topic: -----

#1 wr3ckin_cr3w  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 02-July 09

Passing a vector of structs to a function

Post icon  Posted 05 December 2009 - 03:32 AM

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.


#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Passing a vector of structs to a function

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Passing a vector of structs to a function

Posted 05 December 2009 - 05:16 AM

What problems or errors are you having? aside from the fact that you need to use int main() and not 'void', your code looks just fine.
Was This Post Helpful? 0
  • +
  • -

#3 wr3ckin_cr3w  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 02-July 09

Re: Passing a vector of structs to a function

Posted 05 December 2009 - 09:42 AM

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.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Passing a vector of structs to a function

Posted 05 December 2009 - 10:05 AM

A vector can hold more than 1 Car, although it starts out empty by default, so you first need to add at least one Car to your vector so that you have one to access.

e.g, you might have a function called add_car which adds a car based on some data you've retrieved
void add_car(std::vector<Car>& MVehicle)
{
    Car mycar;  //Temporary 'car' 
    mycar.CarName = "Bob Cratchett";
    mycar.CarYear = 2009;
    mycar.CarNum = 12345;

    MVehicle.push_back( mycar );
} 
the push_back function will insert a copy of the mycar object to the end of the MVehicle vector.


vectors store objects in sequential order - you can access any object in a vector using an index number; be wary that vectors start at index 0, and you will most likely crash your program if you try to access an index which doesn't exist

So, assuming your vector has 1 object in it, this will let you access it
std::cout << "Name: " << MVehicle[0].CarName; 

the square brackets [ ] are called the subscript operator - when you use them after the name of your vector with an index number, you're directly accessing whichever object is at that index number (if one exists).

This post has been edited by Bench: 05 December 2009 - 10:08 AM

Was This Post Helpful? 1
  • +
  • -

#5 wr3ckin_cr3w  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 02-July 09

Re: Passing a vector of structs to a function

Posted 05 December 2009 - 10:13 AM

That was it! I tried using that before, but I tried creating an instance of "MVehicle" instead of "car". Wow, you have no idea how long I was stuck on this. Thanks a bunch man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1