passing an array objects to a function

Hello, i've read that arrays are only passed by reference to member functions. I don't have a problem with this ,but what if i want to access a member variable contained in my array of objects by using a member function??. In the code below i've created an array of objects.In one of the array elements(line 18 , 3rd element for example) i want to access the variable 'author', but my IDE says it's unrecognisable in the member function? . please excuse any silly errors , i'm just after a general principle really?. Plus,I have used a constructor but just not shown it here.
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
class book 
{ 
   
    public:
    
    void findAuthor(book bArray[] )


    private :

    string author;
}



book void:: findAuthor(book bArray[])
    {
       bArray[3].author = "John Smith";
    }


void main() 
   
   {

      book bookArray[100];
       

      findAuthor( book bookArray[] );

   }

Your function is defined incorrectly

You meant:

1
2
3
4
void book:: findAuthor(book bArray[])
    {
       bArray[3].author = "John Smith";
    }



There are some other typo's - but I assume they are just mistakes in posting.


Last edited on
Yes, i see the mistake now, any idea how to pass an array of objects to a function and then access the member variables in that function?,
You need to pass the size as an extra parameter when using raw arrays.
Use vectors instead.
okay. That sounds like quite an advanced topic?. i imagine there are many applications where large arrays of objects are declared in 'main', and then ideally you would want to carry out operations( mutator or accessor) to variables of those objects in a function of some sort, not in the 'main' part of the program which i'm doing at the moment?

Do you know any good resources/web sites for vectors?. Vectors are the last topic in my C++ book and i'm only about half way through!!


Start with checking the reference section of this site:

http://cplusplus.com/reference/
http://cplusplus.com/reference/stl/vector/
Topic archived. No new replies allowed.