gpt4 book ai didi

c++ - 似乎无法调用我的函数

转载 作者:行者123 更新时间:2023-11-28 06:00:26 27 4
gpt4 key购买 nike

我正在尝试设置将两个数组放在一起但取出数组中的重复项的运算符。我的想法是,我将第一个数组复制到新数组中,然后将不在第一个数组中的“产品”复制到新数组中。我正在尝试调用一个函数来检查“产品”是否在第一个数组中,但我不断收到错误消息:错误:成员函数“er_varan_eins”不可行:“this”参数的类型为“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;
}

最佳答案

因为 inv1 是一个 const Inventory &,你只能调用它的 const 成员函数。您的 er_varan_eins 函数可以设为 const,因为它不会修改 Inventory 类的任何成员。

关于c++ - 似乎无法调用我的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33405940/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com