错误:没有匹配功能?

时间:2016-05-26 23:23:49

标签: c++ c++11

问题是,我一直收到此错误>错误:没有匹配函数来调用'std :: basic_ifstream :: basic_ifstream(std :: basic_string)'

和>错误:没有匹配函数来调用'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;
    }
}
}

2 个答案:

答案 0 :(得分:0)

默认情况下,您的编译器似乎没有启用当前标准。在c ++ 11标准std::ifstream::open()仅使用const char*参数之前。

正如我的评论中所提到的,使用-std=c++11编译器标志(或适合您的编译器的任何选项)启用当前标准。

答案 1 :(得分:-1)

fstream :: open采用const char *,而不是字符串。在参数:

上调用.c_str()
file.open(("c:\\" + username + ".txt").c_str());

http://www.cplusplus.com/reference/fstream/fstream/open/