C++

2013-04-02 11 views
5
fstream hatası

Gerçekten yardımınıza ihtiyacım var. Görünüşe göre C++ 'da dosya manipülasyon yapılamıyor. Bazı dosya işleme için fstream kullanılan ama bunu derleme yaparken, bir hata demek görünür olun: hata neC++

|63|error: no matching function for call to 'std::basic_fstream<char>::open(std::string&, const openmode&)'| 

yaptık mı?

İşte kaynak kodunun bir parçasıdır:

#include<stdio.h> 
#include<iostream> 
#include<fstream> 
#include<string>  

using namespace std; 

inline int exports() 
{ 
string fdir; 
// Export Tiled Map 
cout << "File to export (include the directory of the file): "; 
cin >> fdir; 
fstream fp; // File for the map 
fp.open(fdir, ios::app); 
if (!fp.is_open()) 
    cerr << "File not found. Check the file a file manager if it exists."; 
else 
{ 
    string creator, map_name, date; 
    cout << "Creator's name: "; 
    cin >> creator; 
    cout << "\nMap name: "; 
    cin >> map_name; 
    cout << "\nDate map Created: "; 
    cin >> date; 
    fp << "<tresmarck valid='true' creator='"+ creator +"' map='"+ map_name +"' date='"+ date +"'></tresmarck>" << endl; 
    fp.close(); 
    cout << "\nCongratulations! You just made your map. Now send it over to [email protected] for proper signing. We will also ask you questions. Thank you."; 
} 
return 0; 
} 

cevap

6

dosya adı C++ 11 yılında eklenmiştir olarak std::string türünü kabul fstream::open(). Ya -std=c++11 bayrakla derleyin ya da argüman olarak fdir.c_str() kullanın (bunun yerine const char* iletin).

if (std::cin >> fdir) 
{ 
    std::fstream fp(fdir, std::ios::app); // c++11 
    // std::fstream fp(fdir.c_str(), std::ios::app); // c++03 (and c++11). 
    if (!fp.is_open()) 
    { 
    } 
    else 
    { 
    } 
} 
+0

Çok teşekkürler hmjd! Bu çalıştı. Sanırım bunu C++ 11'e derlemedim. –

4

Sen C++ 11 mod std::basic_fstream<char>::open(std::string&, const openmode&) aşırı yük DAİREMİZ olmak için etkinleştirmeniz gerekir: dosya adıyla verdiyse fstream() yapıcı fp.open() çağrısını ortadan kaldıracak olan dosyayı açabilecek

Not .

Geçiş gcc için bunlardan biri:

-std=c++11 veya

-std=c++0x önce 11 C++, istream::open işlevleri sadece Cı dizgileriyle aldı. (Bunu fp.open(fdir.c_str(), ios::app); numaralı telefondan arayabilirsiniz)

+0

Teşekkürler jrok, bu aynı zamanda hmjd'nin cevaplarından da çok yararlandı. :) –