0

I'm a c++ student and this is my first post here.

I have an array containing Member objects (which will be dynamic in the future). I'm trying to pass the array into a function, getLogin in my case.

I think I have some of the syntax wrong, I'm still struggling to understand dynamic arrays and correct syntax for pointers in different situations. Visual Studio is showing an error with myMembers, where it is written as a parameter for getLogin.

#include <iostream>
#include <string>
#include "Member.h"
using namespace std;

int getLogin( const int, Member[] );

int main(){

    int numAccounts = 0;
    int accCapacity = 5;
    int currentAcc = 0;

    Member member[5];

    currentAcc = getLogin( numAccounts, member );
    return 0;
}

int getLogin( const int lastAcc, Member[] myMember ){
    int accNum;
    cout << "account number:" << endl;
    cin >> accNum;

    if( accNum > 0 && accNum <= lastAcc ){
        myMember[accNum].setLoggedIn( true );
    }
    else{
        accNum = 0;
    }
    return accNum;
}

(p.s. What I really want is a pointer to the array, because I don't want a copy of the entire array to be created. However, I believe that using the array name is actually like using a pointer to the first element. So I think I don't need to worry about that.)

Rengetsu
  • 25
  • 7
  • Possible duplicate of: http://stackoverflow.com/questions/17604164/c-how-to-pass-array-of-objects-into-function –  Apr 22 '15 at 22:25
  • Hmm.. I'm reading that question now. However, it looks like their answer suggests using this format for the function: int getLogin( const int lastAcc, Member (&myMemberArr)[] ) But I'm not sure what to put in for the size of the array.. – Rengetsu Apr 22 '15 at 22:34

2 Answers2

2

Change the definition of getLogin() as follows:

int getLogin( const int lastAcc, Member myMembers[] ){
    //...
}

(And of course you should better use std::vector or std::array, but since this is a homework, this advice probably makes no sense)

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
0

If you want to pass an array of pointers then you have to change the implementation of your function getLogin:

int getLogin( int lastAcc, Member** myMembers )
{
    int accNum;

    if( accNum > 0 && accNum <= lastAcc )
    {
        myMembers[accNum]->setLoggedIn( true );
    }
    else{
        accNum = 0;
    }
    return accNum;
}

and this is how you pass an array to it:

int numAccounts = 0;
int accCapacity = 5;
int currentAcc = 0;

Member* members[numAccounts];
for ( int i =0; i < numAccounts; i++)
{
  members[i] =  new Member();
}

currentAcc = getLogin( numAccounts, members )
Christian Abella
  • 5,747
  • 2
  • 30
  • 42
  • it doesn't seem that he wants an array of pointers, he just wants to have the array of objects accessible in the function. – M.M Apr 22 '15 at 22:55
  • @Matt you're correct. I wasn't trying to pass in an array of pointers, but it's interesting to see it as an example. Thanks! – Rengetsu Apr 22 '15 at 23:03
  • if your application will be dynamic in the future then passing pointers may be the way to go. You will be creating a new instance of the same object everytime. So passing it around as a pointer is much efficient. – Christian Abella Apr 22 '15 at 23:05
  • Oh I see. Thank you, I'll have to think about how to implement a dynamic array of pointers then :) – Rengetsu Apr 22 '15 at 23:18