【发布时间】:2016-05-26 23:23:48
【问题描述】:
问题是,我不断收到此错误 > 错误:没有匹配函数调用 'std::basic_ifstream::basic_ifstream(std::basic_string)'
and >error: no matching function for call to 'std::basic_ofstream::open(std::basic_string)'
我该如何解决这个问题?
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
bool loginOn(){
string username, password, un, pw;
cout << "Username > ";
cin >> username;
cout << "Password > ";
cin >> password;
ifstream read("c:\\" + username + ".txt");
getline(read, un);
getline(read, pw);
if ( un == username && pw == password){
return true;
}
else{
return false;
}
}
int main(){
int choice;
cout << "1: Register" << endl;
cout << "2: Login" << endl;
cout << "Your chouce > ";
cin >> choice;
if (choice == 1){
string username, password;
cout << "Choose username > ";
cin >> username;
cout << "Choose password > ";
cin >> password;
ofstream file;
file.open("c:\\" + username + ".txt");
file << username << endl << password;
file.close();
main();
}
else if (choice == 2){
bool status = loginOn();
if (!status){
cout << "Wrong login information" << endl;
system("PAUSE");
return 0;
}
else{
cout << "Congratulation! You've login successfully" << endl;
system("PAUSE");
return 1;
}
}
}
【问题讨论】:
-
你确定你启用了
-std=c++11? -
试试
file.open(std::string("c:\\") + username + ".txt"); -
@πάνταῥεῖ 如何启用它?
-
@πάνταῥεῖ file.open(std::string("c:\\") + username + ".txt"); - 给了我另一个错误
-
@πάνταῥεῖ 启用 -std=c++11 已修复