Hey guys,

I was wondering how I'd go about returning a "list" of strings inside a member function. I'm assuming an array would have to be created, but I'm not sure...I've tried looking it up but anything with "string" Google'd leads to the string class.

Please, I'd really prefer not to show the code (unless requested) because someone usually does it for me and I'd rather just see an example or get a clear understanding. No offense, please, but if someone does it for me, I get de-motivated and start trying to do it another way. OCD here...

string Test::getNames();
{
    string y = "dog";
    string x = "happy";
    string d = "night";
}

What I need is for them to be outputted in int main. Sorry for not making it clear right off the bat...

void Test::getNames(string[] names)
{
     names[0] = "test1";
     names[1] = "test2"
     return;
}

Hi,

Why not make an array of strings and return to a pointer.


So rather you have

string * Test::getNames(string[] names)
{
    string y[0] = "dog";
     y[1] = "happy";
     y[2] = "night";"
     return y; 
}

that might work.

Hi,

Why not make an array of strings and return to a pointer.


So rather you have

string * Test::getNames(string[] names)
{
    string y[0] = "dog";
    string y[1] = "happy";
    string y[2] = "night";"
     return y; 
}

that might work.

Incorrect syntax.

When u write string y[0] it declares an array of string with 0 elements and if u want to initialize them write

string y[] = {"dog", "cat"};

Incorrect syntax.

When u write string y[0] it declares an array of string with 0 elements and if u want to initialize them write

string y[] = {"dog", "cat"};

Yes, My bad.

It is also likely to print out trash along with the "dog, cat". I refer to the method I suggested.

Or just return an array of strings, whichever one, both do the same
job and depending on the context, one might be better to use than the
other.

Sorry to continue bothering, but I still can't "get it"...

#include <iostream>
using namespace std;

class Test
{
      private :
              string name;
       public :
              Test(string);
              ~Test();
              string getName();     
};

string Test::getName(string[] a)
{
       string a[] = {"cookie", "hello"};
       
       return a;
}

int main()
{
      Test a;
      
      a.getName();
      
      return 0;    
}

I've tried many techniques and nothing worked..

string getName(); in the declaration should be void getName(string []);
(you're going to pass it in by pointer so you won't have to return anything)
in main() declare a string mystr[5]; (or however big you want it)
and call a.getName(mystr); Then you can access mystr directly in once you're back in main.

u can make it as

string* Test::getNames()
{
        string *names = new string[3];   
        names[0] = "cat";
        names[1] = "dog";
        names[2] = "rat";
        return names;
}

/*and in main*/
int main()
{
   Test t;
   string *names;
   names = t.getNames();
   cout<<names[0];
   cout<<names[1];
   cout<<names[2];
   return 0;
}
#include <iostream>
using namespace std;

class Test
{
      private :
              string name;
       public :
              Test();
              ~Test();
              void getName(string[]);     
};

void Test::getName()
{
       string a[] = {"cookie", "hello"};
}

int main()
{
      Test a[];
      string mystr[];
      
      a.getName(mystr);
      
      return 0;    
}

Any closer? Still getting an error with the return statement...

void Test::getName( string a[])
{
       a[0] = "cookie";
       a[1] = "hello";
}

Since we're not initializing them anymore we can't use an init list.

dkalita's method would work too

(random addl info)
You need dimensions on your string array, you don't need an array of T, in main and you need to put {} after Test() and ~Test() instead of ;

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.