Hey guys, i am having a problem passing my array into a function . What i am trying to do is to pass my array called Alphabets[] from my main into a function called Key. THe issue i am having is that i keep getting an error telling me that there is no matching function call. I hope for some help in this area.


main.cpp

#include "KarmaSutra.h"
using namespace std;

int main() {

    KarmaSutra  a;
    int MaxSize = 26 ;
    string s =  "d";
    string Alphabets[26] = { "a" , "b" , "c" , "d" , "e" , "f", "g" , "h" , "i"
 , "j" , "k" , "l" , "m"  , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "z" , "y" , "z" } ;

    a.Key(Alphabets[26]) ;

KarmaSutra.h

class KarmaSutra
{
public :
    void Key ( string [26]);
    string AlphaArray[26] ;


};
void KarmaSutra::Key( string d[])
{
  //  d[] =  AlphaArray[ ] ;

  for (int i = 0 ; i <26 ; i ++)
    {
        cout << d[i] << endl;
    }

}

Hey guys, i am having a problem passing my array into a function . What i am trying to do is to pass my array called Alphabets[] from my main into a function called Key. THe issue i am having is that i keep getting an error telling me that there is no matching function call. I hope for some help in this area.


main.cpp

#include "KarmaSutra.h"
using namespace std;

int main() {

    KarmaSutra  a;
    int MaxSize = 26 ;
    string s =  "d";
    string Alphabets[26] = { "a" , "b" , "c" , "d" , "e" , "f", "g" , "h" , "i"
 , "j" , "k" , "l" , "m"  , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "z" , "y" , "z" } ;

    a.Key(Alphabets[26]) ;

KarmaSutra.h

class KarmaSutra
{
public :
    void Key ( string [26]);
    string AlphaArray[26] ;


};
void KarmaSutra::Key( string d[])
{
  //  d[] =  AlphaArray[ ] ;

  for (int i = 0 ; i <26 ; i ++)
    {
        cout << d[i] << endl;
    }

}

Hi gregarion,

You just need to pass the array like this: "a.Key(Alphabets);"

When you pass an array into a function, it's always passed by reference. That is, all the function gets is the address of the start of the array, and not actually any data. So if your array is 26 strings long or a million, it's still passed the same way.

So in fact, even though this compiles: "void Key ( string [26]);", the "26" is actually discarded and it's misleading to put that in. Just have "void Key (string [])".

-Greywolf

thanks alot! its solved!

Just a side note, in your situation you should probebly switch your array to a char type array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.