Visual C++ Weird Compiler errors
I'm trying to make an encryptor/decryptor using XOR method. I have done this before in Console C++, and I'm new to VC++, so I hope you can help me. The encryption works like this:
key = 3
x = 5 // The item being encrypted
encryptedx = x ^ key // the "^" is XOR
So now, I want to make it in VC++, to make this look better that the Console window. In the design view in VC++, I have two rich text boxes, key edit box, and some buttons to start the process. It sounds really easy, but I get these errors:
------ 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> 开发者_StackOverflow社区 [
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 ==========
And here is the code:
#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;
}
}
}
};
}
I really hope some of you can and want to help me.
It looks like you are trying to assign a managed System::String^ object to a std::string. You would need to convert the System::String first:
http://msdn.microsoft.com/en-us/library/1b4az623(v=vs.80).aspx
Also, just to note, this isn't just standard "VC++". Your code is C++/CLI (which may be fine if that's what you're trying to do).
Hope this helps.
For VS2008 and newer, you can use marshal_as
. Otherwise, see @Scott's answer.
#include <msclr/marshal_cppstd.h>
String^ foo = "";
std::string bar = marshal_as<std::string>(foo);
Ok I have gotten home and tried some stuff out for you.
I have this code working on my machine no problem. Put together a little demo on how to convert from std::string to system::String^, as well as from system::string^ to std::string.
While reading, it seems that this is not the best idea going from std::string to system::string^. You could read up on it if you like.
You will also need to set your Common Language Runtime support to /clr. Good Luck, and hope this helps!
#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;
}
精彩评论