Something with a typedef I thought would work...

Started by
4 comments, last by Sol Blue 16 years, 5 months ago
Hi all, I am trying to use the following code:

typedef std::basic_string<TCHAR> tstring;
...
std::ifstream file("Strings.txt");

if (file.is_open())
{
   tstring line = _T("");
   std::getline(file, line);
}

But it gives me three errors that, as far as I can tell, are complaining about having a typdef'd type in that getline:

error C2784: 'std::basic_istream<_Elem,_Traits>&std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'

error C2782: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : template parameter '_Elem' is ambiguous
        c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\string(525) : see declaration of 'std::getline'
        could be 'wchar_t'
        or       'char'

error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
        c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\string(473) : see declaration of 'std::getline'

Has anyone run into this before, or know what I am doing wrong? Thanks a lot!
Advertisement
Actually, those errors look more like you didn't include <fstream> or <iostream>.
Including iostream cleared two of those up, thanks!

Another place recommended I use a wifstream instead of ifstream, which I will probably do since I want to be able to read a Unicode file. However, even with wifstream, I'm still getting an error. Here's the new code and error:

typedef basic_string<TCHAR> tstring;...std::wifstream file(_T("Strings.txt"));	if (file.is_open())	{		tstring line = _T("");		std::getline(file, line);	}


error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char *,std::_Iosb<_Dummy>::openmode,int)' : cannot convert parameter 1 from 'const unsigned short [12]' to 'const char *'        with        [            _Elem=wchar_t,            _Traits=std::char_traits<wchar_t>,            _Dummy=int        ]


Now, obviously it wants ANSI char's, but that seems silly given that it's designed to read Unicode. Does anyone have another way to do this?

(If this is beyond the scope of the For Beginners forum, sorry; if a mod so judges, would you kindly move it to General Programming?)
Strange. The following:

#include <tchar.h>#include <fstream>#include <iostream>#include <string>typedef std::basic_string<TCHAR> tstring;int main(){	std::wifstream file(_T("hello.txt"));		if (file.is_open())		{			tstring line = _T("");			std::getline(file, line);		}	}


compiles fine with VS2005EE as long as the project settings are set to Use Unicode.

Do you have the same headers included as above?
The reason for this is simple (and stupid). The C++ standard states that the fstream classes all take narrow characters strings for file names no matter what kind of character they are designed to read. This is annoying, obnoxious and frankly bizarre, but it's how the standard reads. You'll need to use a narrow character string for your filename. Ways to do this include, not using the _T() macro for your filename parameter, or if the string is produced at runtime, using the wcstombs() function or the ctype facet of a std::locale object to convert from a wide to narrow character string.
EasilyConfused, SiCrane, thanks a lot to you both. I'm actually using VS .NET 2003, so maybe that accounts for the difference between our code? But I think I will take SiCrane's suggestion and just leave the file name in ANSI. I sort of forgot that, since that's one of the "data" files for the game, I can name it whatever I want and have it be constant across all languages. Oops. : /

This topic is closed to new replies.

Advertisement