1

In my homework I have to make the FooCl class for these:

int main()
{
    int x[] = {2, 3, 7, 9, 21, 11, 54, 91};
    FooCl<int> itemsI(x, sizeof(x) / sizeof(x[0]));

    std::string s[] = {"Car", "Bus", "Taxi", "Bike"};
    FooCl<std::string> itemsS(s, sizeof(s) / sizeof(s[0]));

    itemsI.mySort();
    itemsS.mySort();

    if (x[1]!=3 || x[2]!=7 || x[5]!=21 ||
            s[1]!="Bus" || s[2]!="Car" || s[3]!="Taxi") //Notice that x and s don't start sorted, but the if expects them to be sorted after calls to itemsI.mySort() and itemsS.mySort()
    {
        std::cout << "You failed" << std::endl;
    }else{
        std::cout << "You did great" << std::endl;
    }
}

The goal is to make the if function return "You did great". In this task, it is forbidden to change anything in the main function, and I am only allowed to create the FooCl class. The sorting and the validations are calling different elements, so I think I have to use some kind of reference (shallow copy), but I am not sure how to.

Here is my code so far:

template <typename T>
class FooCl
{
private:
    T mItems;
    int mItemsSize;

public:
    FooCl(T (&items)[], int itemsSize)
    {
        this->mItemsSize = itemsSize;
        this->mItems = items;
    };

    ~FooCl(){}

    void mySort()
    {
        std::sort(mItems, mItems + mItemsSize);
    }
};

int main()
{
    int x[] = {2, 3, 7, 9, 21, 11, 54, 91};
    FooCl<int> itemsI(x, sizeof(x) / sizeof(x[0])); //error: no matching function for call to 'FooCl<int>::FooCl

    std::string s[] = {"Car", "Bus", "Taxi", "Bike"};
    FooCl<std::string> itemsS(s, sizeof(s) / sizeof(s[0]));

    itemsI.mySort(); 
    itemsS.mySort();

    if (x[1]!=3 || x[2]!=7 || x[5]!=21 ||
            s[1]!="Bus" || s[2]!="Car" || s[3]!="Taxi")
    {
        std::cout << "false" << std::endl;
    }
    else
    {
        std::cout << "true" << std::endl;
    }
}

At the moment I am getting a compilation error:

    no matching function for call to 'FooCl<int>::FooCl
Dodande
  • 97
  • 5
  • The exercise is not expecting you to take the first constructor parameter by-reference. It expects you to take it by simple pointer `T*`. The size of the array is given as second argument. You are then (maybe?) supposed to simply store the pointer and size in the class and hand them to `std::sort`. – user17732522 May 19 '22 at 16:08
  • 2
    `T mItems` looks like it should be `T* mItems` since `mItems` is supposed to represent an array whose size isn't known at compile time. `delete[] mItems;` in `~FooCl()` is incorrect. Easiest way to see this is to notice that there's no corresponding `new[]`. `FooCl` doesn't take ownership of the array it's handed in its constructor, it shouldn't try to delete it. – Nathan Pierson May 19 '22 at 16:09
  • 1
    @user17732522 No, I think the asker's understanding of creating a shallow copy is correct. Note that `x` and `s` don't start sorted, but the calling code expects them to be sorted after calls to `itemsI.mySort()` and `itemsS.mySort()`. Storing a copy of the whole array wouldn't help. – Nathan Pierson May 19 '22 at 16:11
  • @NathanPierson Yes, you are right. I didn't read the code carefully enough. But then it is a weird exercise and it also has nothing to do with creating a list container as the title seems to indicate. – user17732522 May 19 '22 at 16:12
  • 1
    With C++20 and without any further requirements on the class simply `template struct FooCl { T* a; int b; void mySort() { std::sort(a, a + b); } };` will work. Without C++20 you will have to add a constructor which just initializes the two members from the arguments directly. – user17732522 May 19 '22 at 16:18
  • @Nathan Pierson You are right. The `delete[] mItems;` left in from a previous try where I used `mItems = new T[itemsSize];` I edited my code. – Dodande May 19 '22 at 16:38
  • `T mItems;` will become `int mItems` - maybe not what you are looking for. – AndersK May 19 '22 at 17:03

0 Answers0