c++操作符重载另一个类(对象和操作符来自类A,使用类b的数据)



我不熟悉c++类、对象,尤其是操作符。

给定以下main函数,我必须绕过它,以便最终,程序将编译并正确运行。下面是main函数:

int main()
{
TOperator *oper = new TOperator("Alexandru");
TComanda *command = new TComanda("online", oper);
command += new Item("usb stick", 220); // this is calling Item's constructor 
command += new Item("tastatura", 175); 
oper->afiseazaCommanda();// printing out the final cart
return 0;
}

目前我面临的主要问题是:

1)我如何重载+=操作符,以便我可以将字符串相加(就像在python中为两个字符串做+时一样)并对我的两个整数求和?

2)我应该如何从TOperator写我的afiseazaComanda()函数,使它能正常工作?因为,到目前为止,当试图编译它时,它给了我一个错误">

未在此范围内声明Item

。我试图得到一些关于global operator overloading的信息,但没有找到太多。

搜索SO并没有真正的帮助,因为它并没有以这种方式讨论操作符的过度收费,因此类TComanda的对象可以过度收费类Item,所以我希望我能在那里找到一些明确的帮助。

下面是我的类的代码:
class Item
{
public:
string Product;
int Price;
Item(string product, int price)
{
Product = product;
Price = price;
}
friend class TComanda;
};
class TOperator
{
string Name;
public:
TOperator(string n)
{
Name = n;
}
void afiseazaComanda()
{
cout << Item.Product << " " << Item.Price; // supposed to print out the final cart with both the products and summed prices
}
friend class TComanda;
};

class TComanda
{
string online, name;
public:
TComanda(string onoroff, TOperator *op)
{
online = onoroff;
this->name = op->Name;
}
TComanda operator +=(Item &a)
{
a+=a.Product;
a+=a.Price;
return *this;
} // still thinking how I could write that part of the code
};

不能为指针实现自定义操作符,只能为对象实现。因此,例如,由于您将command声明为TComanda*指针,因此必须对command解引用才能访问它所指向的TComanda对象,例如:

*command += new Item("usb stick", 220);

除了你有TComanda:::operator+=声明为Item&引用,而不是Item*指针,所以你也必须取消对Item*指针的引用:

*command += *(new Item("usb stick", 220));

这是一个内存泄漏,因为您正在失去对new'ed的Item对象的访问权,所以当您完成使用它时,您无法delete它(事实上,您的整个main()正在泄漏new's的每个对象)。因此,去掉new:

*command += Item("usb stick", 220);

在这种情况下,您也可以在commandoper上删除new:

TOperator oper("Alexandru");
TComanda command("online", &oper);
command += Item("usb stick", 220);
command += Item("tastatura", 175); 

话虽这么说,你的整个代码设计是不完整的,因为TOperator没有TComanda的概念,但即使它有,TComanda也不会将其添加的Item存储在TOperator可以到达的地方。

试试这个:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Item
{
public:
string Product;
int Price;
Item(const string &product, int price);
};
class TComanda;
class TOperator
{
string Name;
TComanda *Command = nullptr;
public:
TOperator(const string &name);
void afiseazaCommanda() const;
friend class TComanda;
};
class TComanda
{
string Online, Name;
vector<Item> Items;
TOperator &Operator;
public:
TComanda(const string &onoroff, TOperator &op);
~TComanda();
TComanda& operator +=(const Item &a);
friend class TOperator;
};
Item::Item(const string &product, int price)
{
Product = product;
Price = price;
}
TOperator::TOperator(const string &name)
{
Name = name;
}
void TOperator::afiseazaCommanda() const
{
if (Command) {
for(const auto &item : Command->Items) {
cout << item.Product << " " << item.Price << endl;
}
}
}
TComanda::TComanda(const string &onoroff, TOperator &op)
: Operator(op)
{
Online = onoroff;
Name = Operator.Name;
Operator.Command = this;
}
TComanda::~TComanda()
{
if (Operator.Command == this)
Operator.Command = nullptr;
}
TComanda& TComanda::operator +=(const Item &a)
{
Items.push_back(a);
return *this;
}
int main()
{
TOperator oper("Alexandru");
TComanda command("online", oper);
command += Item("usb stick", 220);
command += Item("tastatura", 175); 
oper.afiseazaCommanda();
return 0;
}

在线演示

更新:

考虑到您在main()中对代码的限制,请尝试这样做:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Item
{
public:
string Product;
int Price;
Item(const string &product, int price);
};
class TComanda;
class TOperator
{
string Name;
TComanda *Command = nullptr;
public:
TOperator(const string &name);
void afiseazaCommanda() const;
friend class TComanda;
};
class TComanda
{
string Online, Name;
vector<Item*> Items;
TOperator *Operator;
public:
TComanda(const string &onoroff, TOperator *op);
~TComanda();
TComanda& operator +=(Item *a);
friend class TOperator;
};
Item::Item(const string &product, int price)
{
Product = product;
Price = price;
}
TOperator::TOperator(const string &name)
{
Name = name;
}
void TOperator::afiseazaCommanda() const
{
if (Command) {
for(const auto *item : Command->Items) {
cout << item->Product << " " << item->Price << endl;
}
}
}
TComanda::TComanda(const string &onoroff, TOperator *op)
{
Online = onoroff;
Operator = op;
Name = Operator->Name;
Operator->Command = this;
}
TComanda::~TComanda()
{
for(auto *item : Items)
delete item;

if (Operator->Command == this)
Operator->Command = nullptr;
}
TComanda& TComanda::operator +=(Item *a)
{
Items.push_back(a);
return *this;
}
int main()
{
TOperator *oper = new TOperator("Alexandru");
TComanda *command = new TComanda("online", oper);
*command += new Item("usb stick", 220);
*command += new Item("tastatura", 175); 
oper->afiseazaCommanda();
delete command;
delete oper;
return 0;
}

在线演示

相关内容

  • 没有找到相关文章

最新更新