I wan't to implement the following code - the check if the pointer is null or not null. If the pointer points to object, then do sth with that object, if not - skip that code block.
My code:
ref class EchoClient {
private:
GameMatrix^ gameMatrix;
public:
EchoClient(void);
EchoClient(GameMatrix^);
void do();
};
EchoClient::EchoClient(void)
{
this->gameMatrix = NULL;
}
EchoClient::EchoClient(gameMatrix)
{
this->gameMatrix = gameMatrix;
}
void EchoClient::do() {
if(this->gameMatrix != NULL)
{
this->gameMatrix->redrawMatrix();
}
}
The error:
error C2446: '!=' : no conversion from 'int' to 'GameMatrix ^' k:\visual studio 2010\Projects\EchoClient3WS\EchoClient3WS\EchoClient.cpp 106
Any solutions ???
nullptr
Related
Code
BOOL CGrAllObjects::ReorderObj(CGrObject* pGrFind,int ixObjNewIx)
{
int nSubCode,nLyrCode=pGrFind->GetLayerCode(nSubCode);
CGrObject* pGrObject;
CGrObjectArray*
pGrObjectArray=GetObjArrayFromCode(nLyrCode,nSubCode);
if(!pGrObjectArray) return FALSE;
for(int ixObj=pGrObjectArray->GetSize()-1; ixObj>=0; ixObj--)
{ pGrObject=pGrObjectArray->GetAt(ixObj);
if(pGrObject==pGrFind) break;
}
if(ixObj<0) return FALSE;
if(ixObj!=ixObjNewIx)
{ pGrObjectArray->RemoveAt(ixObj);
pGrObjectArray->InsertAt(ixObjNewIx,pGrFind);
}
return TRUE;
}
Error: 1>c:\xxx\xxx\xxxx\xxxx\xxxx\xxxxx\xxxx.cpp(359) : error C2065:
'ixObj' : undeclared identifier
for(int ixObj ... the variable "ixObj" is only defined in the scope of the for loop and is not known outside.
Define the integer before the loop, and remove the decalration from 'for':
int ixObj;
for(ixObj=...
I'm porting C++ app from Solaris to Linux and I'm stuck with the following error. The code is:
template <class MapSuperClass> class FWPointerMap : public MapSuperClass
{
public:
FWPointerMap()
{
_wipe = false;
}
FWPointerMap(const MapSuperClass* mMap)
{
MapSuperClass::const_iterator it = mMap->begin(); // line 50
while(it != mMap->end())
{
insert(MapSuperClass::value_type((*it).first, (*it).second));
it++;
}
_wipe = false;
}
And I get the following error:
../../framework/fwcore/hdr/FWPointerMap: In constructor FWPointerMap<MapSuperClass>::FWPointerMap(const MapSuperClass*):
../../framework/fwcore/hdr/FWPointerMap:50: error: expected ; before it
../../framework/fwcore/hdr/FWPointerMap:52: error: it was not declared in this scope
I think you just need to add 'typename' to tell the compiler that MapSuperClass::const_iterator is a type:
typename MapSuperClass::const_iterator it = mMap->begin(); // line 50
Because MaySuperClass is a class template parameter, the assumption is that the const_iterator member is a field. Using typename informs the compiler that it is in fact a type.
More information: http://en.wikipedia.org/wiki/Typename#A_method_for_indicating_that_a_dependent_name_is_a_type
_bstr_t::wchar_t*, _bstr_t::char* operators return string of different types.
Do I need to delete or free them? using which function?
After stepping the implementation using debugger, my conclusion is that there is no need to manually delete/free the returned string. The lifetime of the returned string is managed by _bstr_t internally.
See the following snippets from the implementation:
// Extract a const char_t*
//
inline _bstr_t::operator const char*() const throw(_com_error)
{
return (m_Data != NULL) ? m_Data->GetString() : NULL;
}
inline const char* _bstr_t::Data_t::GetString() const throw(_com_error)
{
if (m_str == NULL) {
m_str = _com_util::ConvertBSTRToString(m_wstr);
if (m_str == NULL && m_wstr != NULL) {
_com_issue_error(E_OUTOFMEMORY);
}
}
return m_str;
}
inline void _bstr_t::Data_t::_Free() throw()
{
if (m_wstr != NULL) {
::SysFreeString(m_wstr);
}
if (m_str != NULL) {
delete [] m_str;
}
}
It is also okay to use unnamed _bstr_t as follows because _bstr_t instance is destroyed after the constructor of CString has finished.
CString abc((LPCTSTR)_bstr_t(OLESTR("ABC")));
AfxMessageBox(abc);
I have the following code which copies property values from one object to another objects by matching their property names:
public static void CopyProperties(object source, object target,bool caseSenstive=true)
{
PropertyInfo[] targetProperties = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] sourceProperties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo tp in targetProperties)
{
var sourceProperty = sourceProperties.FirstOrDefault(p => p.Name == tp.Name);
if (sourceProperty == null && !caseSenstive)
{
sourceProperty = sourceProperties.FirstOrDefault(p => p.Name.ToUpper() == tp.Name.ToUpper());
}
// If source doesn't have this property, go for next one.
if(sourceProperty ==null)
{
continue;
}
// If target property is not writable then we can not set it;
// If source property is not readable then cannot check it's value
if (!tp.CanWrite || !sourceProperty.CanRead)
{
continue;
}
MethodInfo mget = sourceProperty.GetGetMethod(false);
MethodInfo mset = tp.GetSetMethod(false);
// Get and set methods have to be public
if (mget == null)
{
continue;
}
if (mset == null)
{
continue;
}
var sourcevalue = sourceProperty.GetValue(source, null);
tp.SetValue(target, sourcevalue, null);
}
}
This is working well when the type of properties on target and source are the same. But when there is a need for casting, the code doesn't work.
For example, I have the following object:
class MyDateTime
{
public static implicit operator DateTime?(MyDateTime myDateTime)
{
return myDateTime.DateTime;
}
public static implicit operator DateTime(MyDateTime myDateTime)
{
if (myDateTime.DateTime.HasValue)
{
return myDateTime.DateTime.Value;
}
else
{
return System.DateTime.MinValue;
}
}
public static implicit operator MyDateTime(DateTime? dateTime)
{
return FromDateTime(dateTime);
}
public static implicit operator MyDateTime(DateTime dateTime)
{
return FromDateTime(dateTime);
}
}
If I do the following, the implicit cast is called and everything works well:
MyDateTime x= DateTime.Now;
But when I have a two objects that one of them has a DateTime and the other has MyDateTime, and I am using the above code to copy properties from one object to other, it doesn't and generate an error saying that DateTime can not converted to MyTimeDate.
How can I fix this problem?
One ghastly approach which should work is to mix dynamic and reflection:
private static T ConvertValue<T>(dynamic value)
{
return value; // This will perform conversion automatically
}
Then:
var sourceValue = sourceProperty.GetValue(source, null);
if (sourceProperty.PropertyType != tp.PropertyType)
{
var method = typeof(PropertyCopier).GetMethod("ConvertValue",
BindingFlags.Static | BindingFlags.NonPublic);
method = method.MakeGenericMethod(new[] { tp.PropertyType };
sourceValue = method.Invoke(null, new[] { sourceValue });
}
tp.SetValue(target, sourceValue, null);
We need to use reflection to invoke the generic method with the right type argument, but dynamic typing will use the right conversion operator for you.
Oh, and one final request: please don't include my name anywhere near this code, whether it's in comments, commit logs. Aargh.
I have one following methods in java:
public native String jniStringMethod();
public String stringMethod(String s) {
Log.d("Testing", "String:" + s);
return s;
}
I am trying to call "stringMethod" method in jniStringMethod() in cpp file in the following way:
jstring Java_ashok_learning_ndk_SampleNDKActivity_jniStringMethod(JNIEnv *env,
jobject obj) {
jstring jstr = env->NewStringUTF("This comes from jni string .");
//jclass clazz = env->GetObjectClass(obj);
jclass clazz = env->FindClass("ashok/learning/ndk/SampleNDKActivity");
if (0 == clazz) {
LOG("clazz class not found!");
}
jmethodID messageMe = env->GetMethodID(clazz, "stringMethod", "(Ljava/lang/String;)Ljava/lang/String;");
if (0 == messageMe) {
LOG("messageMe method not found!");
}
jobject result = env->CallObjectMethod(obj, messageMe, jstr);
LOG("result: %d", result);
const char* str = env->GetStringUTFChars((jstring)result, NULL); // should be released but what a heck, it's a tutorial :)
printf("%s\n", str);
return env->NewStringUTF(str);
}
But it is not getting called..and i am getting log as "messageMe method not found!",means method is not matching with signature...any one can suggest about my mistakes?
Your signture is OK. Are you sure your code is not executed properly even though messageMe is NULL? It has happend to me that my code was running fine despite jmethodID being NULL.