-1
class myItem {
    int key;
    string name;
    
    public:
    // Constructor
    myItem(int key, string name)
    {
        key = key;
        name = name;
    }
};

class myCollection {
    vector<myItem> col;
    public:
        void insert(myItem &i);
        void print();
};

int main() {
    myCollection c;
    int key;
    string name;
    cin >> key;
    cin >> name;
    myItem i = myItem(key, name);
    c.insert(i);
    c.print();
}

When I try to compile this, I get the error: no matching function for call to ‘myItem::myItem()’ and note: candidate: myItem::myItem(int, std::string). candidate expects 2 arguments, 0 provided. How might I fix this? Thanks!

Jessicaaa
  • 25
  • 1
  • 7

3 Answers3

0

First off, I am not a fan of confusing the compiler with similar names. This is terrible practice (and I am not a fan of the java this.local = param)

And even if you can argue the compiler knows what is right, someone maintaining your code will not. So you could at least capitalize or suffic and underscore or something else if you don't want to rename (sometimes I saw prefixing it with in, like inKey, inName as an alternative method which I am all in favor for, the underscore is a good if you don't want to make the name change to stand out, just make sure the underscore is not in front of the variable/parameter name):

int key;
string name;

public:
// Constructor
myItem(int key_, string name_) : key(key_), name(name_) {}

Moving on, I don't get your error but I do get unresolved externals. The problem is myCollection is defined, but not implemented (or rather its methods aren't). Try an empty placeholder using {}.

class myCollection {
vector<myItem> col;
public:
    void insert(myItem &i) {}
    void print() {}
};

This should fix your errors.

TheNomad
  • 892
  • 5
  • 6
  • @πάντα ῥεῖ I like your inline initialization, I didn't want to complicate his code since I think she's a beginner. I'll edit the text to reflect underscores too, I admit they might be a better choice here. – TheNomad Sep 25 '20 at 06:13
  • I just wanted to correct the failing `**` bold specifiers in code formatted text, but well, if I am at it. I also thought about that the constructor initializer list might confuse the OP further, so you're probably right to change that back again. – πάντα ῥεῖ Sep 25 '20 at 06:17
-1

Your Constructor is named Team and not myItem

Replace it with

myItem(int key, string name)
{
  key = key;
  name = name;
}
MatzZze
  • 391
  • 1
  • 7
-2

The vector you are using in myCollection internally uses the default constructor. Try using a pointer of myItem in the vector or specify the default and copy constructors too.

MatzZze
  • 391
  • 1
  • 7
  • See [push_back vs emplace_back](https://stackoverflow.com/questions/4303513/push-back-vs-emplace-back) please.. _"Try using a pointer of myItem in the vector"_ this is certainly the worst advice you can give about that topic. – πάντα ῥεῖ Sep 24 '20 at 22:33