0

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.

cmonnats23
  • 15
  • 1
  • 6

3 Answers3

0

You'll need to return a vector<InputtedInstructions> and not worry about heap allocation.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

The function is declared to return one InputtedInstructions:

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

But its implementation wants to return a pointer to an InputtedInstructions:

InputtedInstructions* InputList = new InputtedInstructions[counter];
hauzer
  • 258
  • 3
  • 12
-1

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

}   
CS Pei
  • 10,869
  • 1
  • 27
  • 46
  • Awesome. Worked perfectly. Cant believe I didnt see that. – cmonnats23 Oct 02 '13 at 15:44
  • @cmonnats23 Don't forget to upvote and select the answers that work for you :) –  Oct 02 '13 at 15:50
  • @KevinCadieux I would upvote but I dont have enough "reputation" to upvote yet. But I did mark yours as the correct one since you were first and it worked! – cmonnats23 Oct 03 '13 at 05:18
  • @cmonnats23 Actually it was John Smith's answer. But thanks for selecting it! I didn't remember you needed reputation to upvote. Sorry about that. –  Oct 03 '13 at 15:16