-2

I'm still new to c++ so this is a learning process for me. Also i know that i should initially use a vector to do this but i have an exercise that specifies an array so i'm trying to write a function that removes all duplicate elements in an array but i receive the error

C2100: illegal indirection

if someone could point me in the right direction

int main()
{       
    int *t;
    int removel[9] = { 1, 1, 1, 2, 3, 4, 5, 6, 6, };
    t = removeAll(removel, 9, 1);

    for (int i = 0; i < 8; i++)
        cout << t[i] << " ";
}

int* removeAll(int list[], int listlength, int removeitem)
{
    int count = 0;
    int* list2;
    int removeindex;
    int length;
    int tempindex;

    for (int i = 0; i < listlength; i++)
    {
        if (removeitem == list[i])
            count++;
    }

    length =  listlength - (count + 1);
    list2 = new int[length];
    int j;
    while (j<=length)
    {
        remove_if(list[0], list[listlength - 1], removeitem);

        for (j = 0; j < length; j++)
            if (list[j] == NULL)// not sure what the remove_if func puts inplace of the removed element
                continue;
            else
                list2[j] = list[j];
    }
    return list2;

}
mpromonet
  • 11,326
  • 43
  • 62
  • 91
cj881
  • 1
  • 2
  • 1
    #1 Don't declare your variables before you need them. – LogicStuff Apr 10 '16 at 20:16
  • Use vectors. I don not see a reason why you would want to use array. And Array's are fixed size. – maxadorable Apr 10 '16 at 20:17
  • its for an exercise for the textbook i'm using, just trying to learn how to accomplish this task. – cj881 Apr 10 '16 at 20:22
  • And after having switched to vectors, consider this: http://stackoverflow.com/questions/36384571/is-there-a-better-alternative-to-stdremove-if-to-remove-elements-from-a-vector – Frank Puffer Apr 10 '16 at 20:22
  • Ok, #2 Format your code, you want to be able to read it. #3 `j` is not being initialized. #4 Read the [reference](http://en.cppreference.com/w/cpp/algorithm/remove) #5 Think about the algorithm. – LogicStuff Apr 10 '16 at 20:23
  • You can't really remove stuff from an array. Best you can do is replace it with something else. In this case you probably want to use the value after the value you want gone. And that means you have to replace the value after with the value after it. etc... Don't forget to update the amount of the array being used when you do this. – user4581301 Apr 10 '16 at 20:47

1 Answers1

-1

Firstable you should calculate length like listlength - count, not listlength - (count + 1).
Then, after list2 = new int[length]; you should copy elements which are different with removeitem and skip other ones. You can do it like this

    int j = 0;
    for (int i = 0; i < listlength; i++) {
        if (removeitem == list[i])
            continue;
        list2[j] = list[i];
        j++;
    }

and return successfully created list2. But you should also know its size. You can do it by creating int tSize in main and pass it to removeAll by link. removeAll will change its value to length. So add int & list2size to the parameters list of removeAll and write list2size = length; before returning list2. Finally, when printing t, change i < 8 to i < tSize.

If you do all this program will work right but don't forget about formatting.

Pavel
  • 5,374
  • 4
  • 30
  • 55