3 Replies - 4086 Views - Last Post: 03 January 2013 - 10:16 AM Rate Topic: -----

#1 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Problem: passing a vector of struct by reference

Posted 03 January 2013 - 09:59 AM

Hi!

I would like to use my vector in a function but the function caller shows that there is a problem. I donīt really know how I can fix this. Thanks for any idea.

Declaration of struct and vector:
struct Person
{
	string sign;
	string firstName;
	string lastName;
	double height;
};

vector<Person> persons;



Functionsprototyp:
void getName(Person[]);


Functionsdefinition:
void getName(Person persons[])
{

...

}


Function caller:
getName(persons);



Is This A Good Question/Topic? 0
  • +

Replies To: Problem: passing a vector of struct by reference

#2 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Re: Problem: passing a vector of struct by reference

Posted 03 January 2013 - 10:07 AM

Hi!

I would like to use my vector in a function but it shows there is a problem with the function caller. I donīt have any idea what that is. Thanks for any help!

Declaration of vector and struct:
struct Person
{
	string sign;
	string firstName;
	string lastName;
	double height;
}

vector<Person> persons;




Functionsprototype:
void getName(Person[]);



Functionsdefinition:
void getName(Person persons[])
{
...
}



Function caller:
getName(persons);


Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: Problem: passing a vector of struct by reference

Posted 03 January 2013 - 10:12 AM

The problem is that you have a vector, which is an object, and you have a function prototype which expects an array (not an object, etc.).

If you change:
void getName(Person[]);


To something like so:
void getName(std::vector<Person>);


And change the function in any other place that is needed, it should stop complaining.

Also, this is just passing by value, but changing it to pass by reference is as easy as adding & in the correct place. I will leave you to attempt that one.

Further, you have some problems with your existing code:
struct Person
{
	string sign;
	string firstName;
	string lastName;
	double height;
}

vector<Person> persons;



There needs to be a semicolon at the end of a class or struct definition:
struct Person
{
	string sign;
	string firstName;
	string lastName;
	double height;
};

vector<Person> persons;



It is also generally considered bad practice to have global variables (which is what the persons vector appears to be). There are places where it can make life a lot easier (or is required outright), but this isn't one of those, and will actually have a better chance of confusing you down the road.

Hope that helps.
Was This Post Helpful? 2
  • +
  • -

#4 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 18-November 12

Re: Problem: passing a vector of struct by reference

Posted 03 January 2013 - 10:16 AM

Thanks! I realized also quite fast it was a dumb question. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1