【发布时间】:2020-02-25 19:09:03
【问题描述】:
编辑:将其更改为只有一个问题,感谢您的反馈!
我有这个向量
vector<Artifact> art;
art.emplace_back("Ipad", 349.99);
art.emplace_back("Gameboy", 29.99);
art.emplace_back("Xbox", 229.99);
art.emplace_back("SamsungTV", 559.99);
art.emplace_back("AirCon", 319.99);
这些项目给我一个错误
C2661 'Artifact::Artifact': no overloaded function takes 2 arguments
我不明白为什么它会给我这个错误,我有一个带有 7 个参数的构造函数,但我只需要名称和价格来完成我想要做的事情。
编辑:这是最小的可重现示例:
class Item {
public:
virtual double GetTotalPrice();
};
//Class artifact now inherits Item
class Artifact : public Item
{
private:
string GUID;
string Name;
string Description;
string Category;
double Price;
double Discount;
enum DiscountType { Amount, Percentage };
int Quantity;
public:
//Constructor
Artifact(string GUID, string Name, string Description, string Category, double Price, double Discount, int Quantity)
{
this->GUID = GUID;
this->Name = Name;
this->Description = Description;
this->Category = Category;
this->Price = Price;
this->Discount = Discount;
this->Quantity = Quantity;
}
//default constructor
Artifact();
void set_name(const string& name)
{
Name = name;
}
void set_price(double price)
{
if (Price > 0)
{
Price = price;
}
else cout << "Price cannot be negative!";
};
int main()
{
vector<Artifact> art;
art.emplace_back("Ipad", 349.99);
art.emplace_back("Gameboy", 29.99);
art.emplace_back("Xbox", 229.99);
art.emplace_back("SamsungTV", 559.99);
art.emplace_back("AirCon", 319.99);
return 0;
}
【问题讨论】:
-
排序问题见this。对于您的编译器错误,我们需要minimal reproducible example
-
你尝试了什么?您需要做的只是比较您想要排序的成员,但在交换元素时交换完整元素。抱歉,但不清楚是什么问题。也许如果您显示一些代码可以提供帮助。阅读minimal reproducible example
-
您提出的问题与快速排序无关。关于排序的一切都是上下文,但它与您描述的问题无关,除非您要问多个问题。为什么你认为 7 参数构造函数会在这里帮助你?你想在哪里构建一个有 7 个参数的对象?
-
@FrançoisAndrieux 实际上是两个(不相关的)问题,两个问题的措辞都不是很好,一个是关于快速排序,另一个是关于需要 mcve 来回答的编译器错误
-
每个问题请专注于一个问题
标签: c++ object vector quicksort