-1

I know that similar questions have been asked already, but I could not find the answer by looking at similar posts. Here is a minimal working example of my problem with the following C++ code:

#include <iostream>
#include <cstdio>
#include <fstream>

using namespace std;

class File{

 public:
  fstream value;
  string name;
  unsigned int number_of_lines;
  
  
};


void print_filename(File file){

  cout << "Name of file is " << file.name << "\n";
  
}


int main(void){

  File file;
  print_filename(file);
  

  cout << "\n";
  return(0);
    
}

When I compile, I get the error:

example.cpp: In function ‘int main()’:
example.cpp:28:22: error: use of deleted function ‘File::File(const File&)’
   print_filename(file);
                      ^
example.cpp:7:7: note: ‘File::File(const File&)’ is implicitly deleted because the default definition would be ill-formed:
 class File{
       ^~~~
example.cpp:7:7: error: use of deleted function ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const std::basic_fstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
In file included from example.cpp:3:0:
/usr/local/include/c++/7.2.0/fstream:925:7: note: declared here
       basic_fstream(const basic_fstream&) = delete;
       ^~~~~~~~~~~~~
example.cpp:18:6: note:   initializing argument 1 of ‘void print_filename(File)’
 void print_filename(File file){
      ^~~~~~~~~~~~~~

Do you know why? Thank you for your help

Juan
  • 107
  • 4
  • 7
    `print_filename()` receives a *copy* of the file object, which is almost certainly not what you want. Be thankful the compiler found your bug for you and use `print_filename(File& file)` instead so that the function receives a *reference* to the file. –  Jul 22 '21 at 19:03
  • https://stackoverflow.com/questions/7903903/c-copy-a-stream-object – n. m. could be an AI Jul 22 '21 at 19:09
  • @Frank: you are wrong, sending a copy of the file object is what I want. Do not guess what other people want by pretending that you are in their shoes. – Juan Jul 22 '21 at 19:35
  • 2
    @Juan I said that it's almost certainly what you want because copying a file stream is a *nonsensical* concept, which is why attempting to do so causes a compilation error. It might be what you want to do by some definition of "copy" you have in mind, but whatever that is is not what "copy" means as far as the language is concerned. I wasn't talking about your intent, but about what the code you wrote does. –  Jul 22 '21 at 19:41
  • 2
    @Juan library design **requires** _guessing what people want_. And you are asking about standard library behavior. It was already guessed, decades ago, that you should not want to copy an `fstream` object. – Drew Dormann Jul 22 '21 at 19:44
  • I disagree. I do not pretend to be in your shoes and to tell you what you meant to say when you wrote your comment (like you did). I look at the words which you wrote, which cannot not refer to what the code that I wrote does. In fact, if they did, you would not have written 'almost certainly'. – Juan Jul 22 '21 at 20:16
  • @Juan Can you explain why you want to pass a copy of a file object to a function that prints the name of that file? And what do you think should happen when you copy a `fstream` object? If you want to copy a `File` object you need to write a custom copy constructor. – Kevin Jul 22 '21 at 20:31
  • 2
    @Juan you might be upset by people guessing what you want to do, but they are trying to help you reach a solution to your problem. Your question is "Why?" and the answer is "Because you are attempting something that can't be done and isn't necessary, given what you have chosen to show us". You have shown us only broken code, so we must guess what you were attempting. – Drew Dormann Jul 22 '21 at 21:16

1 Answers1

6

Being able to read an error is a valuable skill! Let's do it.


error: use of deleted function ‘File::File(const File&)’

You are calling File's copy constructor, which doesn't exist.

note: ‘File::File(const File&)’ is implicitly deleted

The compiler has implicitly chosen to forbid copy construction of File.

error: use of deleted function ‘basic_fstream(const std::basic_fstream&)

It's because a copy constructor would need fstream's copy constructor, which has been deleted.

 note: declared here
         basic_fstream(const basic_fstream&) = delete;
         ^~~~~~~~~~~~~

That's the code that explicitly states that copy construction is not allowed.

note:   initializing argument 1 of ‘void print_filename(File)’
     void print_filename(File file){

Here is where the problem exists in your code.


The solution, as commented, is to not make a copy. It's not needed.

Pass by reference instead.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180