2013-10-19 41 views
0

我試圖在C++中使用paremeters執行.exe,並且出現錯誤。 .exe執行代碼接近底部。 (resultusernamechars編譯錯誤:模糊過載'operator ='

實際代碼執行:

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

您正在使用C++作爲同GW-Basic中,那些是什麼'goto's? – deepmax

回答

1

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

什麼你可能想要做的是創造result作爲bool

+0

感謝此工作完美 – nulldev

+0

@ user2898813然後將問題標記爲已回答。 – FKaria

+0

對不起,答案很快,網站說我不得不等待。 – nulldev