Visual C ++奇怪的编译器错误

时间:2011-02-08 18:30:52

标签: visual-c++

我正在尝试使用XOR方法制作加密器/解密器。 我以前在Console C ++中做过这个,我是VC ++的新手,所以我希望你能帮助我。 加密的工作方式如下:

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

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

所以现在,我想在VC ++中创建它,使其看起来更好于Console窗口。 在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;
                 }
             }
         }
};
}

我真的希望你们中的一些人可以帮助我。

3 个答案:

答案 0 :(得分:1)

看起来您正在尝试将托管的System :: String ^对象分配给std :: string。您需要首先转换System :: String:

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

另外,请注意,这不仅仅是标准的“VC ++”。您的代码是C ++ / CLI(如果您正在尝试这样做,可能会很好)。

希望这有帮助。

答案 1 :(得分:1)

对于VS2008及更高版本,您可以使用marshal_as。否则,请参阅@ Scott的答案。

#include <msclr/marshal_cppstd.h>

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

答案 2 :(得分: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;
}