如何使用引用复制列表,以使复制版本的更改也适用于C++中的原始版本?

发布于 2025-01-30 19:51:51 字数 1882 浏览 3 评论 0原文

在我的作业中,我必须为这些fuocl班级课程:

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;
    }
}

目标是使if函数返回“您做得很好”。 在此任务中,禁止更改主要功能中的任何内容,而我只能创建FooCl类。 排序和验证正在调用不同的元素,因此我认为我必须使用某种参考(浅副本),但我不确定如何。

这是我到目前为止的代码:

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;
    }
}

目前我遇到了一个编译错误:

    no matching function for call to 'FooCl<int>::FooCl

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文