簡體   English   中英

編譯錯誤:“ operator =”的模棱兩可的重載

[英]Compile error: ambiguous overload for 'operator='

我試圖用參數表在C ++中執行.exe,但出現錯誤。 .exe執行的代碼在底部附近。 resultusernamechars

實際執行代碼:

//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 ===|

您將result聲明為std::string ,然后嘗試將false的值存儲在其中。 result = false;

你的很可能想要做的是創造result作為bool

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM