似乎无法调用我的功能

时间:2015-10-29 04:24:21

标签: c++ arrays

我试图设置将两个数组放在一起但在数组中取出dublicates的运算符。 我的想法是我将第一个数组复制到新数组中,然后复制"产品"不在新数组的第一个数组中。 我试图调用一个函数来检查"产品"是在第一个数组但我不断收到错误: 错误:成员函数' er_varan_eins'不可行:'这个'参数的类型为' const Inventory',但函数未标记为const

这是我的代码:

#include <iostream>

using namespace std;


struct Varan{
    string vara;
    int fjoldi;
    double verd;
};

class Inventory {
public:
    Inventory();
    void stafrofsrod();
    void verdmaetisrod();
    void setsortorder(string rodun);
    void setsorting();
    bool er_varan_eins(const Varan vara1);

    ~Inventory();

    friend istream& operator >> (istream& ins, Inventory &inv);
    friend ostream& operator << (ostream& outs, const Inventory &inv);
    friend Inventory operator + (const Inventory &inv1, const Inventory &inv2);

private:
    Varan *vorulisti;
    int n;
    string order;

};

Inventory::Inventory() {
    n = 0;
    vorulisti = new Varan[n];
}

void Inventory::stafrofsrod() {
    Varan tmp;
    int i, j;

    for (i = 1; i < n; i++) {
        tmp.vara = vorulisti[i].vara;
        tmp.fjoldi = vorulisti[i].fjoldi;
        tmp.verd = vorulisti[i].verd;
        j = i;
        while (j > 0 && vorulisti[j - 1].vara > tmp.vara) {
            vorulisti[j].vara = vorulisti[j - 1].vara;
            vorulisti[j].fjoldi = vorulisti[j - 1].fjoldi;
            vorulisti[j].verd = vorulisti[j - 1].verd;
            j--;
        }
        vorulisti[j].vara = tmp.vara;
        vorulisti[j].fjoldi = tmp.fjoldi;
        vorulisti[j].verd = tmp.verd;
    }
}

void Inventory::verdmaetisrod() {
    Varan tmp;
    int i, j;

    for (i = 1; i < n; i++) {
        tmp.vara = vorulisti[i].vara;
        tmp.fjoldi = vorulisti[i].fjoldi;
        tmp.verd = vorulisti[i].verd;
        j = i;
        while (j > 0 && (vorulisti[j - 1].fjoldi * vorulisti[j - 1].verd) < (tmp.fjoldi * tmp.verd)) {
            vorulisti[j].vara = vorulisti[j - 1].vara;
            vorulisti[j].fjoldi = vorulisti[j - 1].fjoldi;
            vorulisti[j].verd = vorulisti[j - 1].verd;
            j--;
        }
        vorulisti[j].vara = tmp.vara;
        vorulisti[j].fjoldi = tmp.fjoldi;
        vorulisti[j].verd = tmp.verd;
    }
}

void Inventory::setsortorder(string rodun) {
    order = rodun;
    setsorting();
}

void Inventory::setsorting() {
    if (order == "Value") {
        verdmaetisrod();
    }
    else {
        stafrofsrod();
    }
}

Inventory::~Inventory() {
    delete [] vorulisti;
}

istream& operator >> (istream& ins, Inventory &inv) {
    ins >> inv.n;
    inv.vorulisti = new Varan[inv.n];
    for (int i = 0; i < inv.n; i++) {
        ins >> inv.vorulisti[i].vara;
        ins >> inv.vorulisti[i].fjoldi;
        ins >> inv.vorulisti[i].verd;
    }
    inv.stafrofsrod();
    return ins;
}

ostream& operator << (ostream& outs, const Inventory &inv) {
    for (int i = 0; i < inv.n; i++) {
        outs << inv.vorulisti[i].vara << " " << inv.vorulisti[i].fjoldi << " " << inv.vorulisti[i].verd << endl;
    }
    return outs;
}

Inventory operator + (const Inventory &inv1, const Inventory &inv2) {
    int counter = 0;
    for (int i = 0; i < inv1.n; i++) {
        for (int j = 0; j < inv2.n; j++) {
            if (inv1.vorulisti[i].vara == inv2.vorulisti[i].vara) {
                counter++;
            }
        }
    }

    int new_size = inv1.n + inv2.n - counter;
    Inventory new_array;
    new_array.n = new_size;
    new_array.vorulisti = new Varan[new_size];

    for (int i = 0; i < inv1.n; i++) {
        new_array.vorulisti[i].vara = inv1.vorulisti[i].vara;
        new_array.vorulisti[i].fjoldi = inv1.vorulisti[i].fjoldi;
        new_array.vorulisti[i].verd = inv1.vorulisti[i].verd;
    }
    int teljari = 0;
    for (int j = 0; j < inv2.n; j++) {
        if( inv1.er_varan_eins(inv2.vorulisti[j].vara)) {
            teljari++;
        }
        else {
            new_array.vorulisti[j + inv1.n - teljari].vara = inv2.vorulisti[j].vara;
            new_array.vorulisti[j + inv1.n - teljari].fjoldi = inv2.vorulisti[j].fjoldi;
            new_array.vorulisti[j + inv1.n - teljari].verd = inv2.vorulisti[j].verd;
        }
    }
    new_array.stafrofsrod();
    return new_array;
}

bool Inventory::er_varan_eins(const Varan vara1) {
    for (int i = 0; i < n; i++) {
            if (vorulisti[i].vara == vara1.vara) {
                return true;
            }
    }
    return false;
}

int main()
{
    Inventory inv1, inv2;
    cin >> inv1 >> inv2;
    cout << inv1 + inv2;
    return 0;
}

1 个答案:

答案 0 :(得分:1)

由于inv1const Inventory &,因此您只能在其上调用const个成员函数。您的er_varan_eins函数可以const,因为它不会修改Inventory类的任何成员。