代码之家  ›  专栏  ›  技术社区  ›  nulldev

编译错误:“operator=”的重载不明确

  •  0
  • nulldev  · 技术社区  · 11 年前

    我试图用参数在C++中执行一个.exe,但我遇到了一个错误。 执行.exe的代码在底部附近。 ( result username chars )

    实际执行代码:

    //Write result to disk.
    char args[100];
    sprintf(args,"%s %d %d","write.exe",result,username);
    system(args);
    result = false;
    goto start;
    return 0;
    

    完整程序:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <ctime>
    #include <cstdlib>
    #include <sstream>
    #include <time.h>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
        //Declare SOME Variables
        int answer;
        int var_1;
        int var_2;
        int lowest;
        int highest;
        int user_answer;
        int random;
        char username_file,password_file,username,password;
        bool login;
        string result;
    
        //Get Name
        login:
        cout << "Enter your username!" << endl;
        cin >> username;
        cout << "Enter your password!" << endl;
        cin >> password;
    
        //Compare usernames and passwords
        ifstream infile3;
        infile3.open ("database.txt");
        std::string line;
        while (std::getline(infile3, line))
        {
            std::istringstream iss(line);
            int a, b;
            if (!(iss >> username_file >> password_file)) { break; } // error
    
            //Check if username exists.
            if (username_file == username)
            {
                if (password_file == password)
                {
                    cout << "Logged in as " << username << endl;
                    login = true;
                }
            }
            if (login == false)
            {
                cout << "Failed to login with username: " << username << endl;
                cout << "Try again!" << endl;
                goto login;
            }
        }
        infile3.close();
        //Start Tag
        start:
        //Get highest Variable.
        string string_1;
        ifstream infile;
        infile.open ("high.txt");
        getline(infile,string_1);
        infile.close();
        //Get lowest Variable.
        string string_2;
        ifstream infile2;
        infile2.open ("low.txt");
        getline(infile2,string_2);
        infile2.close();
        //Convert Variables
        stringstream ss(string_1);
        ss >> highest;
        stringstream sss(string_2);
        sss >> lowest;
        //Generate Random Numbers
        srand ( time(NULL) );
        var_1=lowest+rand()%(highest);
        var_2=lowest+rand()%(highest);
        //Calculate Answer
        answer = var_1*var_2;
        //Display Variables
        cout <<"What is the product of: "<< var_1 <<" x "<< var_2 << endl;
        cin >> user_answer;
        //Correct?
        if (user_answer == answer)
        {
            cout << "You are correct!" << endl;
            result = true;
        }
        else
        {
            cout << "Incorrect" << endl;
            cout << "The answer is:" << answer << endl;
        }
        //Write result to disk.
        char args[100];
        sprintf(args,"%s %d %d","write.exe",result,username);
        system(args);
        result = false;
        goto start;
        return 0;
    }
    

    错误:

    C:\Users\HC\Desktop\Code\testmultiply.class.cpp||In function 'int main()':|
    C:\Users\HC\Desktop\Code\testmultiply.class.cpp|97|error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'|
    C:\Users\HC\Desktop\Code\testmultiply.class.cpp|99|error: ambiguous overload for 'operator=' in 'result = false'|
    C:\Users\HC\Desktop\Code\testmultiply.class.cpp|99|note: candidates are:|
    D:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|543|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
    D:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|551|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
    D:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|562|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
    ||=== Build finished: 6 errors, 0 warnings ===|
    
    1 回复  |  直到 11 年前
        1
  •  1
  •   Caesar    11 年前

    你声明 result 作为一个 std::string 然后你尝试存储 false 里面。 result = false;

    你可能想做的是创造 后果 作为一个 bool