Visual c++ visualc&x2B+;奇怪的编译器错误
我正在尝试使用XOR方法制作一个加密/解密程序。 我以前在控制台C++中做过这方面的工作,我对VC++有新的见解,所以希望大家能帮我。 加密的工作原理如下:Visual c++ visualc&x2B+;奇怪的编译器错误,visual-c++,Visual C++,我正在尝试使用XOR方法制作一个加密/解密程序。 我以前在控制台C++中做过这方面的工作,我对VC++有新的见解,所以希望大家能帮我。 加密的工作原理如下: key = 3 x = 5 // The item being encrypted encryptedx = x ^ key // the "^" is XOR 现在,我想在VC++中实现它,使它看起来比控制台窗口更好。 在VC++的设计视图中,我有两个富文本框、键编辑框和一些按钮来启动该过程。 听起来很简单,但我有以下错误: -
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 ==========
----已启动生成:项目:加密程序,配置:调试Win32------
1> 加密机
1> c:\users\***************c******ncryptor\encryptor\Form1.h(264):错误C2679:二进制“=”:未找到接受“System::String^”类型右操作数的运算符(或没有可接受的转换)
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(707):可以是“std::basic_string&std::basic_string::operator=(std::basic_string&)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(762):或“std::basic_string&std::basic_string::operator=(const std::basic_string&)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(767):或“std::basic\u string&std::basic\u string::operator=(常量元素*)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(772):或“std::basic_string&std::basic_string::operator=(_Elem)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> 尝试匹配参数列表时“(std::string,System::string^)”
1> c:\*********gl\******cryptor\encryptor\Form1.h(281):错误C2679:二进制“=”:未找到接受类型为“System::String^”的右操作数的运算符(或没有可接受的转换)
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(707):可以是“std::basic_string&std::basic_string::operator=(std::basic_string&)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(762):或“std::basic_string&std::basic_string::operator=(const std::basic_string&)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(767):或“std::basic\u string&std::basic\u string::operator=(常量元素*)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> C:\ProgramFiles(x86)\Microsoft Visual Studio 10.0\VC\include\xstring(772):或“std::basic_string&std::basic_string::operator=(_Elem)”
1> 与
1> [
1> _Elem=char,
1> _Traits=std::char_Traits,
1> _Ax=std::分配器
1> ]
1> 尝试匹配参数列表时“(std::string,System::string^)”
======生成:0成功,1失败,0最新,0跳过==========
代码如下:
#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;
}
}
}
};
}
#包括
#包括“crypFunc.cpp”
int键=0;
char tempChar;
int stringLenght=0;
字符串strBuffer=“”;
使用名称空间std;
//
//
//
//
//
私有:系统::无效按钮1\u单击(系统::对象^sender,系统::事件参数^e)
{
如果(键盘框->文本)
{
key=Convert::ToInt32(键箱->文本);
numericUpDown1->Value=键;
}
}
//
//
//
//
私有:系统::无效加密按钮\单击(系统::对象^sender,系统::事件参数^e)
{
EncryptedBox->Text=“”;
strBuffer=解密框->文本;
StringLength=strBuffer.size();
if(strBuffer.size()&键)
{
对于(int i=0;iText=EncryptedBox->Text+tempChar;
}
}
}
私有:系统::无效解密按钮\单击(系统::对象^sender,系统::事件参数^e)
{
解密框->文本=”;
strBuffer=Convert::ToString(EncryptedBox->Text);
StringLength=strBuffer.size();
if(strBuffer.size()&键)
{
对于(int i=0;iText=DecryptedBox->Text+tempChar;
}
}
}
};
}
我真的希望你们中的一些人能够并且想要帮助我 看起来您正试图将托管System::String^对象分配给std::String。您需要首先转换System::String: 另外,请注意,这不仅仅是标准的“VC++”。您的代码是C++/CLI(如果您正试图这样做,那么这可能很好)
希望这能有所帮助。好的,我回家给你试了一些东西 我的机器上有这个代码,没问题。制作一个小演示,介绍如何从std::string转换为system::string^,以及如何从system::string^转换为std::string 阅读时,似乎
#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;
}
#include <msclr/marshal_cppstd.h>
String^ foo = "";
std::string bar = marshal_as<std::string>(foo);