Edit: Changed it to only one question, thanks for the feedback!
I have this vector
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);
These items give me an error
C2661 'Artifact::Artifact': no overloaded function takes 2 arguments
I don't understand why it's giving me this error, I have a constructor with 7 arguments but I only need Name and Price for what I'm trying to do.
Edit: This is the Minimal reproducible example :
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;
}
Basically, the error you are getting is because you have two constructors (one the default taking 0 arguments, and another the 7 parameter version), but you only pass two values to emplace_back
. The values passed to emplace_back
get forwarded to the constructor of Artifact
.
There's two possible ways around this. First, is to create another constructor which just takes in the two values, like so:
Artifact(string Name, double Price) : Artifact("", Name, "", "", Price, 0., 0 ) {}
Alternatively, you can modify your existing 7 parameter constructor to use default values
// note the reordering of parameters here
Artifact(string name, double Price, string GUID= "",
string Description = "", string Category = "",
double Discount = 0.0, int Quantity = 0) { … }