1

Below code does not work and give the compilation error of:

Severity Code Description Project File Line Suppression State Error invalid operands to binary expression ('std::wofstream' (aka 'basic_ofstream >'))

And the code is:

template <class T>
void printtofile(std::string filename, int mode, T var, std::wstring msg)
{
    wofstream outfile;
    if (mode == 0) outfile.open(filename); else outfile.open(filename, ios::out | ios::app);
    outfile << msg << L"\n";
    outfile << var << L"\n";
    outfile.close();
}

If I comment out the below line there is no error.

    outfile << var << L"\n";

Okay. The weird and confusing thing is that If I add the function with different parameters as the following there is no error although I do not comment out the line I mentioned above:

template <class T>
void printtofile(std::string filename, int mode, T var)
{
    wofstream outfile;
    if (mode == 0) outfile.open(filename); else outfile.open(filename, ios::out | ios::app);
    outfile << var << L"\n";
    outfile.close();
}

Isn't this the same thing? What is going on here?

KC_
  • 89
  • 11

2 Answers2

1

If outfile << var << L"\n"; fails to compile, it's because var is of a type that doesn't have an overload like:

std::wofstream& operator<<(std::wofstream&, const T&);

If you for example try to pass a std::string to the function, that would cause the same error.

To make the error clearer for a user of the function, you can use SFINAE to only instantiate the template for supported types:

template<class T>
auto printtofile(const std::string& filename, int mode, T var,
                 const std::wstring& msg)
    -> decltype(std::wofstream{} << var, void()) {
//...
}

If you now try to use it with an unsupported type, the error will become something like:

error: no matching function for call to
 ‘printtofile(..., int, <the offending type>, std::wstring&)’
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Thank you very much for your answer. Reproducing have been another problem for me. I am working on it. Regarding the line: outfile << var << L"\n"; It works fine in the second code part (overloaded function). This is weird. – KC_ Apr 03 '20 at 12:10
  • You are welcome! Reproduce it by adding a `main()` where you call your function with the variable that causes the compilation error. I suspect that you haven't disclosed the full error message in the question. It _should_ reveal the type, so copy/paste it into the question in full. If the other function succeeds, you are not using the same type when calling that function. – Ted Lyngmo Apr 03 '20 at 12:13
  • You were right. I did not paste whole the error message because I thought it was not related. But after reproduced the code, I understood that It was related with it. I used Armadillo for matrix operations and it creates the problem.However It does not create the error if I use ofstream instead of wofstream. – KC_ Apr 03 '20 at 13:38
  • @KC_ I _think_ you may be mixing types in your code that aren''t readily convertible - but I'm not sure since you've not made it clear yet. Make a small program that presents the problem and put another question up with that [mcve] and people will usually jump at it. – Ted Lyngmo Apr 03 '20 at 22:49
  • 1
    Yes you are right again; I used wstring and string types, together. And I realized wofstream does not print special unicode chacacters. Now I convert wstring to utf8 string and use ofstream instead. – KC_ Apr 05 '20 at 15:15
1

You need to overload << operator for type of var & for std::string you can reduce it to C style string and bypass the error.

Example:

outfile<<msg.c_str()<<L"\n";

Gaurav
  • 1,570
  • 5
  • 17