-4
ofstream out_data1, out_data2;
vector<ofstream> {out_data1, out_data2};

Error image : compiling error is detected Why is this happening? How can I solve it?

Polo Li
  • 23
  • 3
  • 3
    If you don't take the effort to even paste the code and error message into the question, you're not going to get much help. – cigien Jul 03 '20 at 20:04
  • 1
    ofstream copy constructor is deleted – kesarling He-Him Jul 03 '20 at 20:07
  • Sorry guys. Don't know how to copy paste a red-underlined error but the image contains all the information needed. – Polo Li Jul 03 '20 at 20:07
  • 2
    @Elijay, copy constructor is deleted. Move ctor is not... – kesarling He-Him Jul 03 '20 at 20:12
  • The error seems to tell you that the copy constructor of a base class of ofstream is deleted (a way to forbid its use). And your vector definition seems to be invoking that copy constructor. First, I would name the vector. Second, try using a vector of ofstream&. Third, do you really need that? – rturrado Jul 03 '20 at 20:16
  • To get plain text: compile the program then copy the build output and paste it into the question. Formatting the error message as a code block often helps keep the message readable. The compiler is the final arbiter and spends the time to make sure the results are as correct as possible and provide a complete error message. Sometimes pages and pages of of complete error message. The squiggly underlines and pop-up messages are helpful, but they aren't always correct. This one is correct, but precision matters and you should provide the compiler output. – user4581301 Jul 03 '20 at 20:16

1 Answers1

2

If you take a careful look at this link, you will understand that you are trying to copy an ofstream object, and the copy constructor of ofstream is deleted. That is what the error is telling you. You could have simply searched for the error on Google though ;)

kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
  • So "vector {out_data1, out_data2};" actually creates two new objects which copies the value of the original objects? – Polo Li Jul 03 '20 at 20:16
  • 1
    Of course. You are using the [brace-initialisation](https://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives) – kesarling He-Him Jul 03 '20 at 20:18
  • 1
    @PoloLi Note that the compiler may optimize out the copying and construct in place, but the object must still be copy-able – user4581301 Jul 03 '20 at 20:22
  • 1
    In this case I'm thinking more [the As-if rule](https://en.cppreference.com/w/cpp/language/as_if). I think there may be too many layers involved here for honest-to-god copy elision. TL:DR version of the As-if Rule: If there's no change to the observable behaviour, the compiler can do whatever it wants. – user4581301 Jul 03 '20 at 20:25
  • So what I can do is to "vector.pushback() " all the elements or create a vector of ofstream pointers? Or are there other suggestions? – Polo Li Jul 03 '20 at 20:26
  • 2
    Seems like an xy-problem. What do you actually want to do? – kesarling He-Him Jul 03 '20 at 20:26
  • 2
    @PoloLi You cannot copy a stream, but [you can move it](https://stackoverflow.com/questions/3106110/what-is-move-semantics). – user4581301 Jul 03 '20 at 20:27
  • @d4rk4ng31I just wanna manipulate a bunch of ofstream objects and apply for loops on them. – Polo Li Jul 03 '20 at 20:28
  • @d4rk4ng31 I've learned the basic file handling and the code works well before if I don't create a vector of Ofstreams. What I'm trying to do is to output four csv files. – Polo Li Jul 03 '20 at 20:33
  • 1
    @d4rk4ng31 Ok I'll start to learn that. Thank you very much! – Polo Li Jul 03 '20 at 20:37