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.)

2
  • Possible duplicate of: stackoverflow.com/questions/17604164/… Commented Apr 22, 2015 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.. Commented Apr 22, 2015 at 22:34

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Isn't std:: pointless with: " using namespace std" ? You're right, I'm totally unfamiliar with that haha.
@Rengetsu using namespace std is considered bad practice
@Rengetsu I didn't mean you are unfamiliar with this, just that the use of standard C++ containers is often prohibited for students
Oh I see, that is very interesting. I appreciate you taking the time to say so.
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 )

4 Comments

it doesn't seem that he wants an array of pointers, he just wants to have the array of objects accessible in the function.
@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!
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.
Oh I see. Thank you, I'll have to think about how to implement a dynamic array of pointers then :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.