COM : convert 'const GUID*' to const wchar_t* - c++

I am porting some code from VS to mingw C++ . One of the statements in my code is
CFactoryTemplate g_Templates[1] = {
{&CLSID_SystemClock, CSystemClock::CreateInstance}
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
I am getting the following error on the first statement
error: cannot convert 'const GUID* {aka const _GUID*}' to 'const
WCHAR* {aka const wchar_t*}' in initialization
I am completely puzzled by this. I did a little investigation and noticed that
CFactoryTemplate is a class in combase.h . Also my project is UNICODE enabled if that matters. Any suggestions on how to resolve this issue ?

Your code - you say you are porting is wrong, you need to provide different parameters to CFactoryTemplate, the compiler error proves that. Here you will find some sample code to init such array of instances of this class (you dont need to fill all fields):
// list of class ids and creator functions for class factory
CFactoryTemplate g_Templates[2]=
{ { L"Gargle filter" // CFactoryTemplate.m_name
, &CLSID_Gargle // CFactoryTemplate.m_ClsID
, CGargle::CreateInstance // CFactoryTemplate.m_lpfnNew
, NULL // CFactoryTemplate.m_lpfnInit
, &sudGargle // CFactoryTemplate.m_pAMovieSetup_Filter
}
, { L"Gargle filter property page"
, &CLSID_GargProp
, CGargleProperties::CreateInstance
}
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
from https://msdn.microsoft.com/en-us/library/aa451506.aspx
also Hans Passant has given you other example

Related

C++ make::shared wrapping on existing raw pointer

I am working on a small wrapper for an FMOD library. Below is a snippet of the code that I have, and there are other libraries that manage the SoundData class:
class SoundData
{
public:
...
std::string mSoundName;
std::shared_ptr<FMOD::Sound> mFmodSoundHandle;
...
}
void SoundLib::CreateSound(SoundData& data)
{
FMOD::Sound *sound = nullptr;
system->createSound(data.mSoundName.data(), FMOD_DEFAULT, nullptr, &sound);
data.mFmodSoundHandle = std::make_shared<FMOD::Sound>(sound);
}
Trying to compile this snippet of the code, I get this error:
Error C2664 'FMOD::Sound::Sound(const FMOD::Sound &)': cannot convert argument 1 from 'FMOD::Sound *' to 'const FMOD::Sound &' SoundLib ...\MSVC\14.29.30133\include\xutility 158
I cannot quite understand if I am using std::make_shared() in the wrong way here? The goal here is to save the output from createSound() that is passed as a sound variable into the structure SoundData. Variable data will be managed afterwards.
You're passing a pointer where you should pass a reference. Try *sound.
Notice that you're not wrapping a pointer, you're creating a new instance of Sound and copying the value of *sound into it.
To wrap it consider:
data.mFmodSoundHandle.reset(sound);

Error C2664 'void IVerify::SetParams(void)': cannot convert argument 1 from 'std::wstring' to 'wchar_t *'

When I am calling SetParams function from the below function, it is throwing an error "cannot convert argument 1 from 'std::wstring' to 'wchar_t *'"
Can anyone please help me on this?
int main()
{
IVerify* pReader = new BCReader();
std::wstring oemPathKey;
pReader->SetParams(oemPathKey, L"read");
delete pReader;
return 0;
}
void BCReader::SetParams(wchar_t* wszParams, wchar_t* wszParamType)
{
m_wszParamType = wszParamType;
m_wszParams = wszParams;
}
The member variables are declared like as shown below:
class IVerify
{
private:
wchar_t* m_wszParams;
wchar_t* m_wszParamType;
};
There are two parts of the right answer:
1. You need to use pReader->SetParams(oemPathKey.c_str(), L"read");
2. Your approach is not safe, you trying to keep pointer to string in the class members.
But if the original string will go out of scope then you will receive Access Vioalation, if you are lucky :). So in SetParams you need to copy source string to the class members uisng for example wscpy (I recommend something like wscpy_s), also you need correctly handle allocation/deallocation for string copy.

Map insert results in C2664 error in VS 2015, works in VS 2013

This piece of code was working perfectly in VS 2013 but I had to update to VS 2015 and now it throws an error.
I did read https://msdn.microsoft.com/en-us/library/s5b150wd.aspx and googled quite a bit however I still have no idea how to fix this.
I'm using eigen math library to do some 3d math stuff. Eigen's Vector3d class cannot be used as a key to containers so I created my own Vector3dLite class to get around this issue.
class Vector3dLite
{
public:
float VertX, VertY,VertZ;
Vector3dLite(Vector3d& InputVert)
{
VertX = static_cast<float>(InputVert.x());
VertY = static_cast<float>(InputVert.y());
VertZ = static_cast<float>(InputVert.z());
}
Vector3dLite(Vector3dLite& InputVert)
{
VertX = InputVert.VertX;
VertY = InputVert.VertY;
VertZ = InputVert.VertZ;
}
//more operator overloading stuff below
}
Here's where compiler throws the error
map<Vector3dLite, int> VertexIds;
int unique_vertid = 0;
VertexIds.insert(make_pair(Vector3dLite(tri.Vert1), unique_vertid)); //This line
// Vert1 is an eigen Vector3d object
//...
Here's the compiler error:
error C2664: cannot convert argument 1 from 'std::pair<Vector3dLite,int>' to 'std::pair<const _Kty,_Ty> &&'
with
[
_Kty=Vector3dLite,
_Ty=int,
_Pr=std::less<Vector3dLite>,
_Alloc=std::allocator<std::pair<const Vector3dLite,int>>
]
and
[
_Kty=Vector3dLite,
_Ty=int
]
I did try writing const before Vector3dLite object but apparently syntax is not correct.
VertexIds.insert(make_pair(const Vector3dLite(tri.Vert1), unique_vertid));
Since the value type for a map has const object as the first element (the map key), you generally can't use make_pair to construct the value, as the inferred type will not be const.
You can create a pair with explicit types:
std::pair<const Vector3dLite, int>(Vector3dLite(tri.Vert1), unique_vertid)
You can use the map's type
std::map<Vector3dLite, int>::value_type(Vector3dLite(tri.Vert1), unique_vertid)
Or you can create a named const object to use is make_pair
const Vector3dLite mapkey(tri.Vert1);
make_pair(mapkey, unique_vertid);
One other note: Your constructors should take their parameters by const &.

Unable to add ListViewItem - Windows universal app C++

I'm writing a windows universal app (store app) in C++ and XAML, and I made a listview which I want to add items to. This works perfectly fine when hard-coding the items, but once I want to add them via a loop, this doesn't work anymore. And I get the error
cannot convert argument 1 from 'const char *' to 'Platform::Object ^'
Could anyone tell me what I'm doing wrong? Thank you
My code:
/* This works */
myListView->Items->Append("Hello, world!");
/* This doesn't work */
const char* strarray[] = { "Hello", "World", "Awesome" };
for (int i = 0; i < sizeof(strarray); i++) {
myListView->Items->Append(strarray[i]);
}
You are using a basic datatype for your string array
const char* strarray[]
So maybe the function xxx->Items->Append() needs to receive a managed datatype which would be the following for example:
array<String^>^ strarray = { "Hello", "World", "Awesome" };

Error C2440 '=' : cannot convert from 'cli::array<Type> ^' to 'wchar_t'

I got a little problem with my C++/CLI progamm.
I got a few Char arrays wo work without problems.
Header1:
ref class _CGuid{
static const int CIDGR=37;
public:
array<Char>^ cGuid;
array<Char>^ cUuid;
}
Cpp1 -> contruktor:
cGuid = gcnew array<Char>(CIDGR);
some function:
_CGuid::Type(String^ tmpname,String^ tmpid)
{
pcName=tmpname;
cUuid=tmpid->ToCharArray();
}
So this Works Perfectly fine for me without errors.
How ever This doesn’t work:
Other Header:
ref class CStorage{
public:
array<String^>^ names;
array<Char>^ mac;
Other contruktor
names = gcnew array<String^>(100);
mac = gcnew array<Char>(100);
some function 2:
names[k]=tname;
mac[k]=tmac->ToCharArray(); <-------- Error Line
k++;
This line gets the error:
error C2440: '=' : cannot convert from cli::array<Type> ^ to wchar_t
with
[
Type=wchar_t
]
There is no context in which this conversion is possible
So I really don´t know whats the problem here.
The error says it all, actually. ToCharArray returns an array<Char>, which you try to assign to a single Char (= wchar_t) when accessing mac[k].
Did you maybe mean to assign to mac instead?
mac = tmac->ToCharArray();
If so, then this line is redundant:
mac = gcnew array<Char>(100);
Here you allocate memory for mac which you later throw away when you re-assign mac.
here you copy a CLI array coming from the "ToCharArray" in 1 wchar_t of the Mac array!
mac[k]=tmac->ToCharArray(); <-------- Error Line
as you want an array of Mac Address you must allocate it with
mac = gcnew array<array<Char> >(100);
so now you can affect mac[k]