Search code examples
c++structdynamic-arrays

How to return a dynamic allocated struct of arrays in a method?


I have the following struct and method in the public part of my headerfile:

struct InputtedInstructions
{
    string name;
    string arg1;
    string arg2;
    string arg3;
};
InputtedInstructions get_instruction(vector<string>& text, int count);

then in my cpp file:

      Instructions::InputtedInstructions Instructions::get_instruction(vector<string>& vec, int counter)
{   
    int ListPosition = 0;
    InputtedInstructions* InputList = new InputtedInstructions[counter];
    while (ListPosition != counter)
    {

        string text = vec.at(ListPosition);
        istringstream iss(text);
        string command, arg1, arg2, arg3;

        int CommaAmount = count(text.begin(), text.end(), ',');

        if (CommaAmount == 2)
        {
            while( iss >> command >> arg1 >> arg2 >> arg3)
            { 
                InputList[ListPosition].name = command;
                InputList[ListPosition].arg1 = arg1;
                InputList[ListPosition].arg2 = arg2;
                InputList[ListPosition].arg3 = arg3;
                ListPosition++;
            }
        }
//same thingfor 3 commas, 4, etc. 
        return InputList;

My issue is on that return statement down there. it wants me to add [] to the end of it. but I want to return the entire InputList array. Is there something glaringly obvious I am doing wrong? Thanks for your help.


Solution

  • Change your declaration to

    InputtedInstructions* get_instruction(vector<string>& text, int count);
    

    And make the definition to be

    Instructions::InputtedInstructions* Instructions::get_instruction(vector<string>& vec, int counter)
    {
    
    // blah blah
    
    }