[C++] Am I passing the file correctly?

I'm reposting this here in case it helps for anyone else.

I only got one warning when building (using Visual Studio) and it was only complaining about comparing the int in the for loop the line.size() since that's an unsigned-signed comparison.

I think Microsoft C++ is its own language in a way.

So the thing now is, when I use ifstream as a parameter and not a reference to ifstream, I get the error: "Attempting to reference a deleted function."

I think you don't understand what I was saying. Let me make it simple. Passing an ifstream is like dividing by zero. That's what you're trying to do. I'm assuming you don't know about operator overloading. Basically, you can tell a class what to do when you use an operator, say +. It's a special function for an operator. And () is an operator, called the call operator, and is called when you pass the class to a function. ifstream does NOT have this function, so the behavior is undefined. That's why you can't pass by value with the reference. I assume when you pass by reference it's just giving the memory address or something.

If I leave it with the reference, it still won't execute anything within my while loop. Even after I used infile.open(filename) instead of what I had before. The reason I was passing by reference is so I could open the same file without having to prompt for a file name twice.

So get rid of the reference. You can't pass the ifstream in. Pass the string in, not the ifstream itself.

 

Also, if my advice helped you, I would appreciate if you upvoted my advice, as there's a reason why I had to message you. I kept getting "you are doing that too much". Someone downvoted me just because I had stated that it helps to learn multiple programming languages, and my karma makes it very hard for me to help others.

/r/learnprogramming Thread Parent