# Problem: passing a vector of struct by reference

Page 1 of 1

## 3 Replies - 6026 Views - Last Post: 03 January 2013 - 10:16 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=305464&amp;s=8410512eae5fe041624e7d5a46a1ea3d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 krillov

Reputation: 1
• Posts: 36
• 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

Reputation: 1
• Posts: 36
• 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);

```

### #3 BetaWar

• #include "soul.h"

Reputation: 1299
• Posts: 7,645
• 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.

### #4 krillov

Reputation: 1
• Posts: 36
• 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.