2011-02-08 206 views
0

我想使用XOR方法制作加密器/解密器。我已經在Console C++中完成了這個工作,並且我是VC++的新手,所以我希望你能幫助我。 加密的工作原理是這樣的:Visual C++奇怪的編譯器錯誤

key = 3 
x = 5 // The item being encrypted 

encryptedx = x^key // the "^" is XOR 

所以,現在,我希望把它在VC++,使這更好​​看的控制檯窗口。 在VC++的設計視圖中,我有兩個富文本框,編輯框和一些按鈕來啓動過程。 這聽起來很容易,但是我得到這些錯誤:

------ Build started: Project: Encryptor, Configuration: Debug Win32 ------ 
1> Encryptor.cpp 
1>c:\users\**********c*********ncryptor\encryptor\Form1.h(264): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion) 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(707): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(std::basic_string<_Elem,_Traits,_Ax> &&)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(762): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(767): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(772): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   while trying to match the argument list '(std::string, System::String ^)' 
1>c:\*******gl\*****cryptor\encryptor\Form1.h(281): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion) 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(707): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(std::basic_string<_Elem,_Traits,_Ax> &&)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(762): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(767): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(772): or  'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Ax=std::allocator<char> 
1>   ] 
1>   while trying to match the argument list '(std::string, System::String ^)' 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

這裏是代碼:

#include <string> 
#include "crypFunc.cpp" 

int key = 0; 
char tempChar; 
int stringLenght = 0; 
string strBuffer = ""; 
using namespace std; 

// 
// 
// 
// 
// 



    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
      { 
       if(KeyBox->Text) 
       { 
       key =Convert::ToInt32(KeyBox->Text); 
       numericUpDown1->Value = key; 
       } 


      } 
// 
// 
// 
// 




private: System::Void EncryptButton_Click(System::Object^ sender, System::EventArgs^ e) 
     { 

      EncryptedBox->Text = ""; 

      strBuffer = DecryptedBox->Text; 
      stringLenght = strBuffer.size(); 
      if(strBuffer.size() && key) 
      { 
      for(int i = 0; i < stringLenght; i++) 
      { 
       tempChar = encrypt(strBuffer[i], key); 
       EncryptedBox->Text = EncryptedBox->Text + tempChar; 
      } 
      } 

     } 


private: System::Void DecryptButton_Click(System::Object^ sender, System::EventArgs^ e) 
     { 
      DecryptedBox->Text = ""; 
      strBuffer = Convert::ToString(EncryptedBox->Text); 
      stringLenght = strBuffer.size(); 
      if(strBuffer.size() && key) 
      { 
       for(int i = 0; i < stringLenght; i++) 
       { 
        tempChar = decrypt(strBuffer[i], key); 
        DecryptedBox->Text = DecryptedBox->Text + tempChar; 
       } 
      } 
     } 
}; 
} 

我真的希望你們中的一些可以和想幫助我。

回答

1

它看起來像你試圖分配一個託管System :: String ^對象到一個std :: string。您將需要系統::字符串先轉換:

http://msdn.microsoft.com/en-us/library/1b4az623(v=vs.80).aspx

而且,只是要注意,這不僅僅是標準的「VC++」。你的代碼是C++/CLI(如果這就是你正在做的事情,這可能沒問題)。

希望這會有所幫助。

0

好吧,我已經回家了,並嘗試了一些東西給你。

我有這個代碼在我的機器上工作沒有問題。彙總一個關於如何將std :: string轉換爲system :: String ^,以及從system :: string ^轉換爲std :: string的小演示。

閱讀時,它似乎並不是從std :: string到system :: string ^的最佳主意。如果你喜歡,你可以閱讀它。

您還需要將通用語言運行時支持設置爲/ clr。祝你好運,希望這能幫到你!

#include <string> 
#include <iostream> 
#include <msclr\marshal.h> 
#include <msclr\marshal_cppstd.h> // you will only need one of these 

using namespace std; 
using namespace System; 
using namespace msclr::interop; 

int main() { 
    //define our std::string 
    string standardString = "Test String to Marshal"; 
    cout << "String before conversions: " << standardString<<endl; 

    // convert std::string to system::string^ using marshalling 
    String^ sysStringFromStandardString; 
    sysStringFromStandardString = marshal_as<String^>(standardString); 

    // convert system::string^ to std::string 
    string backToStdString; 
    backToStdString = marshal_as<string>(sysStringFromStandardString); 

    cout<< "String after converstion: "<<backToStdString<<endl; 

    return 0; 
} 
+0

嘿。我理解你的答案,並且在源代碼中設置了它,但它仍然無效。 你能告訴我你將如何解決在System :: Void EncryptButton_Click這個問題? – Janman 2011-02-08 19:28:42

1

對於VS2008及更新版本,您可以使用marshal_as。否則,請參閱@ Scott的答案。

#include <msclr/marshal_cppstd.h> 

String^ foo = ""; 
std::string bar = marshal_as<std::string>(foo);