2

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;

}
BlueTune
  • 1,023
  • 6
  • 18
James
  • 45
  • 6
  • For the sorting problem, see [this](https://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects). For your compiler error, we need a [mre] – NathanOliver Feb 25 '20 at 19:11
  • what did you try? All you need to do is compare only the member you want to sort for but when swapping element swap the complete element. Sorry, but not clear what is the problem. Maybe if you show some code one can help. Read about [mcve] – 463035818_is_not_an_ai Feb 25 '20 at 19:11
  • 1
    The question you are asking has nothing to do with quick sort. Everything about sorting is context but it isn't relevant to the problem you describe, unless you are asking multiple questions. Why do you think a 7 argument constructor will help you here? Where are you trying to build an object with 7 arguments? – François Andrieux Feb 25 '20 at 19:11
  • @FrançoisAndrieux actually it is two (unrelated) questions, both not very well phrased, one is about quick sort the other about a compiler error that would need a mcve to be answered – 463035818_is_not_an_ai Feb 25 '20 at 19:13
  • Please focus on one question per question – 463035818_is_not_an_ai Feb 25 '20 at 19:14
  • "when I try to run the program even though I have a 7-arg constructor." The compiler needs to know what values to use for the remaining 5 arguments when it constructs this object. Either use default values in the constructor declaration, or declare and define a second constructor that takes only two elements. – JohnFilleau Feb 25 '20 at 19:18
  • this is not a minimal reproducible example. It has both too little and too much to reproduce the compiler error: https://wandbox.org/permlink/KJYUaolGvIkTCRd9 – 463035818_is_not_an_ai Feb 25 '20 at 19:18
  • You are passing two arguments to emplace_back, but your constructor takes in 7 values... – ChrisMM Feb 25 '20 at 19:18
  • If you only need two arguments, then set the rest of the parameters to have default values. – ChrisMM Feb 25 '20 at 19:21
  • The true beauty of a [mcve] is in making one you almost always knock down the noise level around the bug enough to easily spot and fix the bug. Related is the common programming recommendation that you only write small bits of code, a few lines or a function, at a time before stopping, compiling , and testing. With only a few lines changed each time, if you insert a bug, you can usually find it in the few added lines. This is very little noise. You also find bugs faster and before they can build up a. Untangling and solving multiple bugs is many times more difficult than solving one bug. – user4581301 Feb 25 '20 at 19:24
  • @ChrisMM So I set the other strings to NULL and the other values to 0? – James Feb 25 '20 at 19:24
  • @James, `NULL` is not a valid value for `string`, but you can do `Artifact(string GUID, string Name, string Description = "", string Category = "", double Price = 0.0, double Discount = 0.0, int Quantity = 0) { … }` – ChrisMM Feb 25 '20 at 19:26
  • With `std::string` you don't have to do anything. It defaults to empty. If you try to initialize a string on `NULL`, you'll get a logic error. The `string` constructor that will be selected is the one expecting a pointer to a `char` array, and that constructor will try to use the char array it expects to find at the `NULL` pointer. The program will probably instantly crash. If not, happy debugging! – user4581301 Feb 25 '20 at 19:27

1 Answers1

2

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) { … }
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • Thanks for the quick replies, you've been really helpful. I did as you said and I'm getting a new error Error C2664 'Artifact::Artifact(Artifact &&)': cannot convert argument 2 from '_Ty' to 'std::string' – James Feb 25 '20 at 19:55
  • Honestly I don't know where I went wrong here. – James Feb 25 '20 at 19:56
  • That's a move constructor, but I'm not sure why you are getting that error. A move only has one argument. – ChrisMM Feb 25 '20 at 21:47