views:

130

answers:

4
void BinaryTree::InitializeFromFile(string Filename){
ifstream inFile;
inFile.open(Filename, fstream::binary);
if(inFile.fail()){
    cout<<"Error in opening file "<<Filename;
    return;
}
 for(int i=0;i<=255;i++) Freq[i]=0;
  char c;
  inFile.get(c);
  while(!inFile.eof()){
    Freq[c] ++;
    inFile.get(c);
  }
}  



HuffmanTree.cpp(293) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::
open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 
from 'std::string' to 'const wchar_t *'
1>    with
1>    [
1>        _Elem=char,
1>        _Traits=std::char_traits<char>
1>    ]
1>    No user-defined-conversion operator available that can perform this 
      conversion, or the operator cannot be called

Line 293 is inFile.open(Filename, fstream::binary);

+1  A: 

use Filename.c_str() instead of Filename in the call of ifstream::open

f4
+3  A: 

Use Filename.c_str() instead - open() doesn't take a std::string as a parameter for the filename.

Timo Geusch
+1  A: 

The ifstream ctor expects a const char *. Use Filename.c_str().

dirkgently
+1  A: 

Somewhat perplexingly, ifstream::open takes a C-string, not a C++ std::string. Change the line to:

inFile.open(Filename.c_str(), fstream::binary);

I have no idea why the designers of the C++ standard library made this choice, but there you go.

Tyler McHenry
Because in general the C++ standard library design is to not force users to use features they don't want to use, such as std::string.
MSN
Fair enough, but to not even provide an overload that accepts `std::string` still strikes me as a bit weird.
Tyler McHenry