Visual C++奇怪的编译器错误
问题描述:
我想使用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;
}
}
}
};
}
我真的希望你们中的一些可以和想帮助我。
答
它看起来像你试图分配一个托管System :: String ^对象到一个std :: string。您将需要系统::字符串先转换:
http://msdn.microsoft.com/en-us/library/1b4az623(v=vs.80).aspx
而且,只是要注意,这不仅仅是标准的“VC++”。你的代码是C++/CLI(如果这就是你正在做的事情,这可能没问题)。
希望这会有所帮助。
答
好吧,我已经回家了,并尝试了一些东西给你。
我有这个代码在我的机器上工作没有问题。汇总一个关于如何将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;
}
答
对于VS2008及更新版本,您可以使用marshal_as
。否则,请参阅@ Scott的答案。
#include <msclr/marshal_cppstd.h>
String^ foo = "";
std::string bar = marshal_as<std::string>(foo);
嘿。我理解你的答案,并且在源代码中设置了它,但它仍然无效。 你能告诉我你将如何解决在System :: Void EncryptButton_Click这个问题? – Janman 2011-02-08 19:28:42