0

I have the following scenario that works in Visual C++ 10 but not with GCC on Linux:

call:

value& v;
wstring fn(L"");
char_conv::str_to_wstr( path, fn );

parse( v, ifstream( fn.c_str() ) ); //<-- ERROR

funct def:

inline std::string parse(value& out, std::istream& is){...}

This is the error I get:

In member function ‘std::string PrintInvoker::extractParameter(const std::string&, picojson::value&)’:
error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const wchar_t*)’
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
cristian
  • 518
  • 5
  • 20
  • 1
    `ifstream` just does not accept wide strings: http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream – Lol4t0 Oct 23 '15 at 12:32
  • You're also trying to bind a temporary to a non-const lvalue reference, which is not allowed. – Emil Laine Oct 23 '15 at 12:35

1 Answers1

1

std::idstream has the following constructors:

basic_ifstream();
explicit basic_ifstream( const char* filename,
            ios_base::openmode mode = ios_base::in );

explicit basic_ifstream( const string& filename,                                  
            ios_base::openmode mode = ios_base::in );

basic_ifstream( basic_ifstream&& other );

basic_ifstream( const basic_ifstream& rhs) = delete;

Now when you call fn.c_str() that returns a wchar_t* since fn is a wstrgin. As you can see there are no overloads that take a wchar_t* so the compiler is giving you an error.

Looking at my copy of MSVS2015 it appears that Microsoft has added a constructor that does take a wchar_t* so that is why it works on there,

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Thank you for helpful explanation. There is a way of converting to the needed type by the idstream's constructor. Or other solution ? – cristian Oct 23 '15 at 13:33
  • 1
    @cristian Either change `fn` to a `std::string` or you can convert it: http://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string – NathanOliver Oct 23 '15 at 13:36