2
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);

BIBD
  • 15,107
  • 25
  • 85
  • 137
Azreal
  • 229
  • 3
  • 6
  • 16

4 Answers4

4

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

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
3

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

f4.
  • 3,814
  • 1
  • 23
  • 30
2

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
  • 74,820
  • 18
  • 121
  • 166
  • 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 Feb 26 '10 at 20:23
  • Fair enough, but to not even provide an overload that accepts `std::string` still strikes me as a bit weird. – Tyler McHenry Feb 26 '10 at 21:12
1

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

dirkgently
  • 108,024
  • 16
  • 131
  • 187