I'm trying to build a function to assign VARIANTARG types to a DISPPARAMS structure, and I'm having some issues figuring out how to check the actual type of the arg.
myFunc(int count, const BYTE* types, ...)
{
DISPPARAMS dParams;
//Initialization stuff goes here
for(int x = 0;x < count;x++)
{
BYTE vt = types[0];
dParams.rgvarg[x].vt = vt;
if(vt == VTS_I4)
{
dParams.rgvarg[x].lVal = ...;
}
}
}
BYTE params[] = {VTS_I4};
myFunc(1, params, 123);
When I compile this, I get two errors at the if statement:
error C2446: '==' : no conversion from 'const char *' to 'int'
error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
So I'm fairly confused here: why can I assign a BYTE value from the VTS_I4, but I can't compare it later? If I cast VTS_I4 to a BYTE then I can compare it, but it has a different value so the if statement isn't triggered. The other thing I can do since VTS_I4 is defined in afxdisp.h as "\x03" is check if(vt == 0x03), and that works fine, but has to be wrong somehow.
Another way to ask this question might be why I can do this:
BYTE a[] = VTS_I4;
but not this:
BYTE b = VTS_I4; //'initializing' : cannot convert from 'const char [2]' to 'BYTE'
Thanks
As mentioned in the comments, the problem was that I was using the wrong constant; I needed to use VT_I4 instead of VTS_I4.
Related
I'm trying to make the program read the text file and use the line for function, but I get an error!
5 IntelliSense: operand types are incompatible ("BYTE" and "char *")
Error 1 error C2446: '==' : no conversion from 'char *' to 'int'
Error 2 error C2040: '==' : 'int' differs in levels of indirection from 'char [260]'
my code:
char* ReadINI(char* szSection, char* szKey, const char* szDefaultValue)
{
char* szResult = new char[255];
memset(szResult, 0x00, 255);
GetPrivateProfileString(szSection, szKey, szDefaultValue, szResult, 255, ".\\Config.ini");
return szResult;
}
int main (Classdata* Cdata)
{
BYTE ByteID = Cdata->ByteType;
static char ReadByte[MAX_PATH];
sprintf(ReadByte, "%s", ReadINI("CONFIG", "Key", "0"));
if (ByteID == ReadByte)
{
printf("Byte Value: %p", ReadByte);
}
}
Firstly, you are comparing ByteId (an unsigned char `) with ReadByte (a char*) which is unlikely to be meaningful.
On an unrelated note, szResult is created with new but never deleted in main.
Couldn't be much clearer ByteId is a BYTE, ReadByte is a char[260]. You cannot compare integers and arrays. Perhaps (only a guess) you meant ByteId == ReadByte[0].
Plus you have a memory leak in ReadIni, and ReadByte is declared static for no good reason I can see, and your declaration of main is not legal.
I used another type of function to read the file! Sorry for the bad explanation is that I'm working on a private project! thanks for the help everyone
fuction:
UINT value = GetPrivateProfileInt("Section", "Key", DEFAULT_VALUE, "program.ini");
thx for all!
I'm trying to use a separate function to read a few data values in from a file; I'm getting two errors (I haven't used c++ in a long time...):
double * FREAD(std::string fpath){
std::string line;
double * params = new double[14];
std::ifstream parameters (fpath);
if (parameters.is_open())
{
for (int b = 0; b < 14; b++) {
parameters >> line >> params[b];
}
}
parameters.close();
return params;
}
throws
error C2556: 'double *FREAD(std::string)' : overloaded function differs only by return type from 'double FREAD(std::string)'
and
error C2040: 'FREAD' : 'double *(std::string)' differs in levels of indirection from 'double (std::string)'
The second issue is thrown from the line where I call the function in main.
double * param = FREAD(parampath);
error C2440: 'initializing' : cannot convert from 'double' to 'double *'
If I don't define param as a value pointed at by a double, I get the same type mismatch error in reverse...
My understanding is that I'm supposed to return a pointer which is directed at the first value of the array my subfunction has created and use that to work with the data. But I can't seem to pull this value when I call the function in main.
The simplest and fool-proof way to do it would be to return a container, for example
#include <array>
std::array<double,14> FREAD(const std::string& fpath)
{
std::string line;
std::array<double, 14> params;
// .... fill params
return params;
}
Concerning the overload error, you cannot overload a function by return type. It looks like the compiler is seeing these two declarations:
double * FREAD(std::string fpath);
double FREAD(std::string fpath);
Given the above suggestion, you should remove both of them anyway.
Your error C2556 is because you apparently have another FREAD() function that returns something else. The error is telling you that you can't overload functions based only on the return type.
Going from the messages, it appears to me that you have two functions:
double * FREAD(std::string fpath)
double FREAD(std::string fpath)
You only posted the first one.
C++ only allows you have two functions with the same name if they take different arguments (or const-ness for member functions). You should either give your functions different names, or pass in a token argument that the compiler can use to tell which one you want.
error C2664: 'CCertStoreHelper::DeleteCtl' : cannot convert parameter 1 from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'std::wstring &error C2664: 'CCertStoreHelper::DeleteCtl' : cannot convert parameter 1 from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'std::wstring &
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>,
_Ax=std::allocator<wchar_t>
]
Conversion loses qualifiers
I have no idea regarding this. So kindly provide the solution.
Code:
CCertStoreHelper certCaStore;
std::set<std::wstring> ctlIdentifiersToRemove; // It populates data which I m not mentioning
std::set<std::wstring>::iterator iter1;
std::set<std::wstring>::iterator iter2;
for(iter1 = ctlIdentifiersToRemove.begin(); iter1 != ctlIdentifiersToRemove.end(); iter1++)
{
iter2 = ctlIdentifiersReferenced.find((*iter1));
if(iter2 == ctlIdentifiersReferenced.end())
{
if(certCaStore.DeleteCtl((*iter1))) // error line
{
// ...
}
}
}
// prototype for DeleteCtl fun is
bool CCertStoreHelper::DeleteCtl(std::wstring &ctlIdentifier)
Kindly correct me what i am doing wrong
Thanks
As twalberg points out, the most important bit of the compiler error message is the "loses qualifiers" bit. It also tells you that it can't convert from const std::wstring to std::wstring&, except that it expanded the first std::wstring into its full template instantiation form.
The issue is that your DeleteCtl takes the argument by non-const reference, as if it wants to modify the string there (bad idea), but it can't do that, because you're iterating over a set, and you cannot change the members of a set once they're in there (std::set doesn't make a difference between const_iterator and iterator, essentially). The reason is that std::set bases its internal structure on the values of its elements, and if you change those values, the internal structure becomes invalid, and horrible things happen.
i am new to C++ and am trying to set an Editbox text with a Int.
Using this code
int MyInt = 100;
SetDlgItemText(MyWindow, EditKills, MyInt);
I am getting this error below
In function 'BOOL DialogProc(HWND__*, UINT, WPARAM, LPARAM)':|
invalid conversion from 'int' to 'const CHAR*'|
Update Solved.
I used this code if anyone else has this problem and needs help.
int MyInt = 100;
SetDlgItemInt(MyWindow, EditKills, MyInt, TRUE);
The answer is the same as the answer to your question Invalid conversion from 'DWORD' to 'const char *'
C++ does not automatically convert any type to a string. You'll have to do that manually with sprintf.
case CSETimer:
int GotValue;
ReadProcessMemory(hProcess, (void*)(MYBASE + 0x728), &GotValue, 4, NULL);
char GotValueStr[10];
sprintf(GotValueStr, "%d", GotValue);
SetDlgItemText(MyWindow, EditKills, GotValueStr); return TRUE;
As per your code the variable GotValue is int . You need to convert into to LPCTSTR and pass it as the third argument of your SetDlgItemText function
char szBuf[MAX_PATH]={0};
_snprintf(szBuf, MAX_PATH-1,"%d",GotValue);
or use itoa_s
or
std::ostringstream oss;
oss <<GotValue;
std::string s(oss.str());
s.c_str()
I have the below program written in C++:
#include <iostream>
using namespace std;
int main()
{
int age[5];
char name[5][10];
age[0]=10;
age[1]=20;
age[2]=30;
age[3]=25;
age[4]=40;
name[0]="abc";
name[1]="abc";
name[2]="abc";
name[3]="abc";
name[4]="abc";
cout<<name[0]<<" is "<<age[0]<<"years old";
cout<<"\n";
cout<<name[1]<<" is "<<age[1]<<"years old";
cout<<"\n";
cout<<name[2]<<" is "<<age[2]<<"years old";
cout<<"\n";
cout<<name[3]<<" is "<<age[3]<<"years old";
cout<<"\n";
cout<<name[4]<<" is "<<age[4]<<"years old";
cout<<"\n\n";
system("PAUSE");
}
When I compile and run it, I get these errors:
error C2440: '=' : cannot convert
from 'const char [3]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
I am running MSVC 2008 under Windows 7. I have tried many possible solutions but I failed in fixing this. Any help would be appreciated,
You are treating the name array as if it was defined thus:
char *name[5];
So either define it that way, or use the following code to populate it:
strcpy(name[0], "abc");
strcpy(name[1], "abc");
strcpy(name[2], "abc");
strcpy(name[3], "abc");
strcpy(name[4], "abc");
I prefer the former choice. The point being you are trying to assign a char * to a char [] which is what strcpy is for. Given you are manipulating initialized C strings in this case anyway, you might as well deal with char * throughout the code.
You should use std::string for this purpose. The use of char* and char[] to represent strings is deprecated in C++ for many good reasons.
Given the program snippet, name can be initialized at the declaration itself.
char name[5][10] = { "abc", "abc", "abc", "abc", "abc" } ;
// ^ index 5 is not necessary. char name[][10] = { .. } would also suffice.
Specified the length of each row is 10 but only using first 3 indexes of it. Every 3rd index ( i.e., 4th element in the array ) is automatically added with a '\0'.
Initialization can be done in case of age array too.
You can use also std::string name[10] instead of 2d char's array. In this case only you can assign new values to the strings through operator '='.
Otherwise you should to use array of char* and use strcpy() function for assignment.