I need to convert from a SQLVARCHAR to a string data type
Variable definitions as follows:
string strFirstName;
SQLVARCHAR rtnFirstName[50];
Want to be able to accomplish the following:
if (strFirstName.empty()) strFirstName = rtnFirstName;
Gives an error that the binary '=': no operator found which takes a right-hand operand of type 'SQLVARCHAR[50]' (or there is no acceptable conversion)
What database API are you using? All the Google hits I can find for SQLVARCHAR say it's an unsigned char, so you can do something like this:
strFirstName = reinterpret_cast<char*>(rtnFirstName);
Related
I'm currently working on a DLL that needs to convert back and forth between the friendly name for a value and the value itself. As this code is used in many places throughout the codebase, I want to try and keep it simple and in a single function or object so I only have to declare them once.
From my reading it looks like CMap is the tool for the job, but I can't seem to discover any combination of template arguments that compiles without errors.
My values are CString and int. I tried the following definition:
CMap<int, int, CString, CString> encodermap;
which compiles, but when I try to add a value:
encodermap["Encoder 1"] = 0;
I get the following compiler errors:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2501: 'encodermap' : missing storage-class or type specifiers
error C2040: 'encodermap' : 'int []' differs in levels of indirection from 'CMap<KEY,ARG_KEY,VALUE,ARG_VALUE>'
I've tried changing the CMap to this:
CMap<CString, CString, int, int> encodermap;
but I get the same four errors.
I'm sure I must be missing something but I'm at a loss as to what.
Because of the SDK being used for this work I'm require VS2003
The problem
I think you've inverted the key type and the valye type.
Your original declaration defines int as being the key to be searched for with operator[]. So encodermap[0] = "Encoder 1"; would work.
But when your compiler sees encodermap["Encoder 1"] = 0;, he tries to find an operator[] which takes char* (or something to which char * can be converted to) and returns an int. The last error message tells you that he couldn't find such an operator for your map.
With MSVC 2015, the error message is more concise: C2679.
The solution
You should define your CMap with a CString key and an int value. The trick to know, is that for a CString KEY, the the ARG_KEY should be LPCWSTR . So the right definition would be:
CMap<CString, LPCWSTR, int, int> encodermap;
This permits to use CString as key in the map's operator[].
Now if you use MFC on windows, you probably use UNICODE and wide chars (therefore the LPCWSTR instead of LPCSTR). When calling the operator you then have either to use a CString or a wide literal:
encodermap[L"Encoder 1"] = 0;
encodermap[CString("Encoder 2")] = 1;
Try this:
CMap<CString, LPCTSTR, int, int> encodermap;
This CodeProject article CMap How-to may be of some help.
Many people get confused about CMap's declaration CMap < KEY, ARG_KEY,
VALUE, ARG_VALUE >, why not just CMap < KEY, VALUE >?
In fact, the ultimate data container in CMap is CPair, and the
internal of CPair is {KEY, VALUE}. Therefore, CMap will really store a
KEY, and not ARG_KEY. However, if you check with the MFC source code,
almost all the internal parameters passing within CMap itself is
called with ARG_KEY and ARG_VALUE, therefore, using KEY & as ARG_KEY
seems always a correct thing, except when:
You are using primitive date types like int, char, where pass-by-value makes no difference (may be even faster) with
pass-by-reference.
If you use CString as KEY, you should use LPCTSTR as ARG_KEY and not CString &, we will talk more about this later.
Edit: Cristophe, another option for the assignment is encodermap[_T("Encoder 1")] = 0;, which will work for single-byte, multi-byte or Unicode with the LPCTSTR typedef. You will also need to #include <tchar.h>.
I am trying to use a custom template for IO, and I getting an error :
"error C2678: binary '>>' : no operator found which takes a left-hand operand of
type 'std::ifstream' (or there is no acceptable conversion)"
I have searched and found only suggestions to try including more headers, and have tried including: string, fstream, iostream, istream, vector. I can use an fstream.get(), but I am trying to get space delimited strings. (The format of my file is lines like this: "String1 = String2")
Here is my code:
template <typename OutType>
OutType Read(std::ifstream& in)
{
OutType out;
in >> out;
return out;
}
Any suggestions are very much appreciated! Thanks!
(P.S. Not sure if it will matter for compiler considerations, but I am using Visual Studio 2013.)
The problem is your OutType (which you have not shown us) has no operator>>(istream&, OutType&). You need to define one for every possible OutType.
How you are expecting OutType is known to >> operator? It understands primitives like int,char, etc., but if you want to make OutType available to << you should overload the operator.
I am using the ldap_modify function to change an attribute value using C++ ...
msgid=ldap_modify_ext_s( ld, dnNameval, ldapmod,NULL,NULL );
The problem is the mod_val argument in that function ...
LDAP *id;
dnNameval ="distinguised name";
In the ldapmod there are three values ...
mod_type="attribute to be changed";
mod_op=LDAP_MOD_REPLACE;
ldapmod struct
The ldapmod struct is:
typedef struct ldapmod {
int mod_op;
char *mod_type;
union {
char **modv_strvals;
struct berval **modv_bvals;
} mod_vals;
#define mod_values mod_vals.modv_strvals
#define mod_bvalues mod_vals.modv_bvals
} LDAPMod;
I tried passing the value like this:
mod_vals=(PWCHAR*){"bala",Null};
which resulted in the error
error C2679: binary '=' : no operator found which takes a right-hand operand
of type 'PWCHAR *' (or there is no acceptable conversion)
I also tried this:
mod_vals.modv_strvals=(PWCHAR*) Password1;
mod_vals.modv_bvals=NULL;
but then the array has a null value...
Can anyone help me work out how to pass the values in that variable? Do I need to include any additional header files? I am already using the winldap.h header.
Thanks in advance.
This won't work:
mod_vals = (PWCHAR*){"bala",Null};
You can't assign a value to a union that way. You have to specify which of the members of the union you want to write to. Also, (PWCHAR *) is the wrong type; you're not dealing with wide characters. Even if you were, you can't convert a string just by casting it.
This won't work either:
mod_vals.modv_strvals=(PWCHAR*) Password1;
mod_vals.modv_bvals=NULL;
The modv_strvals member is a pointer-to-pointer, not just a pointer, and it doesn't make sense to write to two members of a union; they're mutually exclusive. Depending on context you either use modv_strvals or modv_bvals, never both.
You probably want to do this:
mod_vals.modv_strvals = {"bala", Null};
I'm having bizarre behavior with stringstreams. It seems that if I create two stringstreams, one will write correctly and one will raise errors. (test is a char*)
ostringstream s;
ostringstream d;
s<<test<<endl;
d<<test<<endl;
This gives the message "error: invalid operands of types 'int' and 'const char*' to binary 'operator<<'" for the last line.
ostringstream s;
ostringstream d;
d<<test<<endl;
d<<test<<endl;
This gives the message "error: invalid operands of types 'int' and 'const char*' to binary 'operator<<'" for both lines writing to d.
The two streams should be identical, so I don't know why d doesn't work. Switching the order of the declarations of s and d doesn't change anything. Anyone have an ideas why this might happen?
Thanks!
I have the same error when the variable d has already been declared so it has another type.
I'm basically trying to write a basic converter in visual studio 2008, and I have 2 text boxes, one which gets input from the user, and one which gives output with the result. When I press the button I want the input from the first textbox to multiply by 4.35 then display in the 2nd textbox. This is my code in the button code so far:
String^ i1 = textBox1->Text;
float rez = (i1*4.35)ToString;
textBox2->Text = rez;
However I'm getting these errors:
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2676: binary '*' : 'System::String ^' does not define this operator or a conversion to a type acceptable to the predefined operator
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2227: left of '->ToString' must point to class/struct/union/generic type
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(149) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'float' to 'System::String ^'
Please help I'm going insane on how ridiculously difficult it is to get some input from a textbox in C++. I've googled every error I had and nothing useful came up, I've been searching answers for an hour already, please help.
Fixing it for you,
String^ i1 = textBox1->Text;
float rez = (float)(Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();
Basically, you want to convert your string to an actual number, do the math, and then make it back into a string for displaying purposes.
You're trying to multiply a string by a double and there is no operator that defines how to do that. You need to convert your string to a double first, and then use that in the calculation.
Then, you're trying to assign a string to a float, which again is nonsense.. You need to calculate the float, then convert it to a string when assigning it to the textbox text field.
Something like:
String^ i1 = textBox1->Text;
float rez = (Convert::ToDouble(i1)*4.35);
textBox2->Text = rez.ToString();