0

I'm writing a program to manage products, employees and bills of a restaurant in C++. But I have a problem when writing a function to read information of products from file to the Product struct. That is C2280 Error and it say something like my code is attemping to reference a deleted function. I've read for this Error in Google but I don't understand so much. This is an excerpt of my code:

struct Product
{
    string productID;
    string productName;
    string productType;
    int productPrize; //USD
}; 

/* function to get Products information from file */
bool readProductInformation(ifstream f, ProductList& productList)
{
    if (!f.is_open()) {
        cout << "Can not onpen file for reading!" << endl;
        return false;
    }

    f >> productList.numberOfProducts;
    int amount = productList.numberOfProducts;
    if (amount == 0) {
        f.close();
        return true;
    }
    PNode* temp = productList.head;
    for (int i = 0; i < amount; i++)
    {
        string str;
        getline(f, str, '\n');
        stringstream iss(str);

        getline(iss, temp->data.productID, '-');
        getline(iss, temp->data.productName, '-');
        getline(iss, temp->data.productType, '-');
        f >> temp->data.productPrize;


        temp->next = new PNode;
        temp = temp->next;
    }
    temp = NULL;
    f.close();
    return true;
}

When I create ifstream f and productList list and call the function again, it comes to the Error!

  • 1
    You need to pass the stream by reference to your function – AndyG Jul 08 '20 at 02:40
  • Can you explain me more? – babylearnmaths Jul 08 '20 at 02:42
  • 2
    Does this answer your question? [C++ Compiler Error C2280 "attempting to reference a deleted function" in Visual Studio 2013 and 2015](https://stackoverflow.com/questions/31264984/c-compiler-error-c2280-attempting-to-reference-a-deleted-function-in-visual) – Alex Jul 08 '20 at 02:43
  • Possible duplicate? https://stackoverflow.com/questions/31264984/c-compiler-error-c2280-attempting-to-reference-a-deleted-function-in-visual – Alex Jul 08 '20 at 02:43
  • 2
    Please don't approximate errors with loose phrasing ("say something like") and instead *copy-paste the exact error text* you're seeing. – tadman Jul 08 '20 at 02:44
  • Pass the stream by reference instead of by value. – drescherjm Jul 08 '20 at 02:45

1 Answers1

2

The ifstream type has no copy constructor because it was intentionally deleted, meaning that you cannot pass it by value as you are doing in the readProductInformation function. More information about ifstream's constructors can be found here, most importantly the following

(3) copy constructor (deleted) Deleted (no copy constructor).

In order to fix this, just pass the ifstream object by reference instead of by value. This means that the object accessed in the function is actually the same object that is passed in. Changes made to this object are not restricted to inside the function. Here is a good post that goes into more detail about the difference.

So you simply have to change it to

bool readProductInformation(ifstream& f, ProductList& productList)

Why would someone intentionally delete a copy constructor? Because sometimes it doesn't make sense for specific objects to be copied, and if you don't specifically delete an object's copy constructor, the compiler is smart enough to sometimes make one for you allowing you to do what was unintended (copy). Here is a somewhat dense but good source explaining deleting and defaulting functions.

Mason W
  • 81
  • 3