2

I get this error when trying to push back an object of custom type. code follows:

class Item_base    
{
    public:     
    Item_base(string isbn=" ", int num=0):book(isbn),number(num){}    
    Item_base(Item_base &I):book(I.book),number(I.number){cout<<"\nCopy Constructor"<<endl;}    
    Item_base &operator=(Item_base &d){if (this != &d){this->book=d.book;this->number=d.number;cout<<"\nAssignment Operator"<<endl;return *this;}else{cout<<"\nAssignment Operator"<<endl;return *this;}}    
    ~Item_base(){cout<<"Item_base Destructor"<<endl;}   
    protected:    
    string book;     
    int number;    
};


#include <iostream>   
using namespace std;
#include <vector>
#include "Item_base.h"
int main (int argc, char * const argv[]) {    
    // insert code here...    
    vector<Item_base> vecbase;     
    Item_base derivo("Harry Potter",10);     
    cout<<"enter book name, qty, dqty"<<endl;     
    vecbase.push_back(derivo);     
    return 0;   
}

The error message i get is:

error: no matching function for call to 'Item_base::Item_base(const Item_base&)'

Can someone help me with this? I am new to programming

slugster
  • 49,403
  • 14
  • 95
  • 145

1 Answers1

5

Vector has only two implementations of push_back function

  • push_back(const T&)
  • push_back(T&&) (since C++11)

This explains, why we need to provide copy constructor with parameter const T& for our class

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
awesoon
  • 32,469
  • 11
  • 74
  • 99