gpt4 book ai didi

c++ - 编译错误 : ambiguous overload for 'operator='

转载 作者:行者123 更新时间:2023-11-28 07:21:04 28 4
gpt4 key购买 nike

我正在尝试使用参数在 C++ 中执行 .exe,但出现错误。.exe 执行的代码靠近底部。(resultusername 都是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 ===|

最佳答案

您将 result 声明为 std::string,然后尝试将 false 的值存储在其中。 result = false;

您可能想要做的是将 result 创建为 bool

关于c++ - 编译错误 : ambiguous overload for 'operator=' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471372/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com