#1
  1. I [heart] Toxic Waste
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Here
    Posts
    423
    Rep Power
    177

    Cannot convert 'this' pointer from 'struct_name' to 'struct_name &'


    I have a struct....

    Code:
    struct JNI_Interface
    {	
    	JavaVM *jvm;       /* denotes a Java VM */ 
    	JNIEnv *env;       /* pointer to native method interface */ 
    	static jobject phone_obj;   // phone object to make JNI calls to PTF with
    
    	int jni_startJVM();
    	int jni_checkException(char* jni_call) const;
    	int jni_startCom() const;
    };
    Code:
    JNI_Interface::jni_startCom() const
    {
    ...
    }
    and I'm calling functions from it here...

    mobilePhone object....
    Code:
    struct JNI_Interface ptfPhone;
    
    mobilePhone::com_startCom()
    {
    	// Added for JNI interface with PTF
    	if(comPort == "PTF")
    	{
    		return ptfPhone.jni_startCom();
    
    	}	
    ....
    My issue is that I keep getting this error....

    error C2662: 'JNI_Interface::jni_startCom' : cannot convert 'this' pointer from 'JNI_Interface' to 'const JNI_Interface &'

    All the things I've found said that adding the "const" stuff to the functions would take care of this conversion issue, but as you can see it still remains. Any ideas?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    247
    Rep Power
    83
    Can we see your code for JNI_Interface::jni_startCom()? "const" means that you cannot modify any members or call any non-const methods
  4. #3
  5. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,812
    Rep Power
    439
    Originally Posted by spoon!
    Can we see your code for JNI_Interface::jni_startCom()? "const" means that you cannot modify any members or call any non-const methods
    An addition to the jni_startCom() code, also post the declaration for the class if possible -- it will be helpful to see a summary of all the other member functions and data as well.
    OMG RAVER CHICKS!!
    On a related note: C/C++ Programming Tutorials


    "Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
    "Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards
  6. #4
  7. I [heart] Toxic Waste
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Here
    Posts
    423
    Rep Power
    177
    My code for jni_startCom()....

    Code:
    JNI_Interface::jni_startCom() const
    {
    	const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone"; // class of phone to use
    
    	jclass phone_cls = env->FindClass(phone_class_name);
    		jni_checkException("FindClass(SynergyPhone)");
    	jmethodID phone_cid = env->GetMethodID(phone_cls, "<init>", "()V");
    		jni_checkException("get cid for SynergyPhone");
    	phone_obj = env->NewObject(phone_cls, phone_cid);    // create a phone object to use PTF methods on
    		jni_checkException("create phone object");
    
    	// *****  Set up a Properties object for the phone.open(props) method used to open the phone connection *****
    
    	// find the Properties class
    	jclass prop_cls = env->FindClass("java/util/Properties");
    		jni_checkException("FindClass(Properties)");
    
    	// constructor for Properties
    	jmethodID prop_cid = env->GetMethodID(prop_cls, "<init>", "()V"); 
    		jni_checkException("cid for Properties");
    
    	// create new Properties object
    	jobject prop_obj = env->NewObject(prop_cls, prop_cid);
    		jni_checkException("create Properties object");
    
    	// get method ID for Properties.setProperty()
    	jmethodID setProp_mid = env->GetMethodID(prop_cls, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
    		jni_checkException("get mid for Properties.setProperty()");
    
    	// jstring values for CONN = USB_AUTO
    	jstring key = env->NewStringUTF("CONN");
    	jstring value = env->NewStringUTF("USB_AUTO");
    
    	// set the COMM value of the Property object
    	env->CallObjectMethod(prop_obj,setProp_mid,key,value); 
    		jni_checkException("call the setProperty method");
    
    	// ****** call open(props)  *********
    
    	jmethodID open_mid = env->GetMethodID(phone_cls, "open", "(Ljava/util/Properties;)V");
    		jni_checkException("mid for SynergyPhone.open()");
    
    	env->CallVoidMethod(phone_obj,open_mid,prop_obj);
    		if(jni_checkException("call the open() method"))
    			return ACTION_SUCCEEDED;
    		else 
    			return ACTION_FAILED;
    }
    ACTION_SUCCEEDED and ACTION_FAILED are just defined to be 1 and -1 respectively.

    and JNI_Interface is a struct, I listed the declaration in my first post....

    Code:
    struct JNI_Interface
    {	
    	JavaVM *jvm;       /* denotes a Java VM */ 
    	JNIEnv *env;       /* pointer to native method interface */ 
    	static jobject phone_obj;   // phone object to make JNI calls to PTF with
    
    	int jni_startJVM();
    	int jni_checkException(char* jni_call) const;
    	int jni_startCom() const;
    };
  8. #5
  9. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,812
    Rep Power
    439
    Your jni_startCom() function is const. This means that it cannot modify any non-mutable member variables, which in turn means that it can't call any non-const member functions of member objects. For example:

    C++ Code:
    class Something {
    public:
      void someFunction (void);
      void someConstFunction (void) const;
    };
     
    class SomethingElse {
    private:
      Something *a;
      mutable Something *b;
    public:
     
      void function1 (void) {
        a->someFunction(); // ok
        a->someConstFunction(); // ok
        b->someFunction(); // ok
        b->someConstFunction(); // ok
      }
     
      void function2 (void) const {
        a->someFunction(); // wrong. this could modify a, which is not allowed.
        a->someConstFunction(); // ok
        b->someFunction(); // ok. even though it's non-const, b is mutable
        b->someConstFunction(); // ok
      }
     
    }


    Do not abuse the mutable keyword. The mutable keyword may be used on member variables when modifying that variable doesn't have any effect on the conceptual state of the object. For example, let's say you have a Person class with a getFirstName() member. The getFirstName function would be const, since it doesn't really do anything to change the state of a Person:

    Code:
    class Person {
    
    public:
    
      // ...
    
      const string & getFirstName (void) const {
        return _fname;
      }
    
    private:
    
      string _fname;
    
    }
    But now let's say you want to add some statistics. For a given person, you want to keep track of how many times you've retrieved their first name. So you want to maintain a count of getFirstName calls:

    Code:
    class Person {
    
    public:
    
      // ...
    
      const string & getFirstName (void) const {
        ++ _count;
        return _fname;
      }
    
    private:
    
      string _fname;
      int _count;
    
    }
    But alas! The getFirstName() is const, so you can't do that. But _count is just for stats; it has no real effect on anything that has to do with a Person at all. So:

    Code:
    class Person {
    
    public:
    
      // ...
    
      const string & getFirstName (void) const {
        ++ _count;
        return _fname;
      }
    
    private:
    
      string _fname;
      mutable int _count;
    
    }
    And now you can modify _count from const functions, and your intentions are clear since you've explicitly specified "mutable" there (as opposed to doing something sideways like casting away the constness of "this").

    So, in jni_startCom, you will have problems if any of those member functions you are calling on it's member objects (jvm, env, phone_obj), are non-const. That is because if the member object has a non-const function, then said non-const function may modify the member object, which is not allowed since jni_startCom() can't modify any of the JNI_Interface members.

    So, verify that all those functions you call (FindClass, GetMethodID, etc) are all const.

    Hope that helps. :)
    OMG RAVER CHICKS!!
    On a related note: C/C++ Programming Tutorials


    "Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
    "Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    247
    Rep Power
    83
    Originally Posted by DorkRawk
    My code for jni_startCom()....

    Code:
    JNI_Interface::jni_startCom() const
    {
    	const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone"; // class of phone to use
    
    	jclass phone_cls = env->FindClass(phone_class_name);
    		jni_checkException("FindClass(SynergyPhone)");
    	jmethodID phone_cid = env->GetMethodID(phone_cls, "<init>", "()V");
    		jni_checkException("get cid for SynergyPhone");
    	phone_obj = env->NewObject(phone_cls, phone_cid);    // create a phone object to use PTF methods on
    		jni_checkException("create phone object");
    
    	// *****  Set up a Properties object for the phone.open(props) method used to open the phone connection *****
    
    	// find the Properties class
    	jclass prop_cls = env->FindClass("java/util/Properties");
    		jni_checkException("FindClass(Properties)");
    
    	// constructor for Properties
    	jmethodID prop_cid = env->GetMethodID(prop_cls, "<init>", "()V"); 
    		jni_checkException("cid for Properties");
    
    	// create new Properties object
    	jobject prop_obj = env->NewObject(prop_cls, prop_cid);
    		jni_checkException("create Properties object");
    
    	// get method ID for Properties.setProperty()
    	jmethodID setProp_mid = env->GetMethodID(prop_cls, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
    		jni_checkException("get mid for Properties.setProperty()");
    
    	// jstring values for CONN = USB_AUTO
    	jstring key = env->NewStringUTF("CONN");
    	jstring value = env->NewStringUTF("USB_AUTO");
    
    	// set the COMM value of the Property object
    	env->CallObjectMethod(prop_obj,setProp_mid,key,value); 
    		jni_checkException("call the setProperty method");
    
    	// ****** call open(props)  *********
    
    	jmethodID open_mid = env->GetMethodID(phone_cls, "open", "(Ljava/util/Properties;)V");
    		jni_checkException("mid for SynergyPhone.open()");
    
    	env->CallVoidMethod(phone_obj,open_mid,prop_obj);
    		if(jni_checkException("call the open() method"))
    			return ACTION_SUCCEEDED;
    		else 
    			return ACTION_FAILED;
    }
    ACTION_SUCCEEDED and ACTION_FAILED are just defined to be 1 and -1 respectively.

    and JNI_Interface is a struct, I listed the declaration in my first post....

    Code:
    struct JNI_Interface
    {	
    	JavaVM *jvm;       /* denotes a Java VM */ 
    	JNIEnv *env;       /* pointer to native method interface */ 
    	static jobject phone_obj;   // phone object to make JNI calls to PTF with
    
    	int jni_startJVM();
    	int jni_checkException(char* jni_call) const;
    	int jni_startCom() const;
    };
    I don't see any problems with this. Maybe post code from the file that calls jni_startCom()?

    Originally Posted by peenie
    So, verify that all those functions you call (FindClass, GetMethodID, etc) are all const.

    Hope that helps. :)
    This is not relevant. env is a pointer, and he calls env->someMethod, which is a method on the object that is pointed to, which can never change the pointer.

    Comments on this post

    • peenie agrees : dang rep'd out but ill get ou later
  12. #7
  13. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,812
    Rep Power
    439
    Originally Posted by spoon!
    This is not relevant. env is a pointer, and he calls env->someMethod, which is a method on the object that is pointed to, which can never change the pointer.
    Indeed! I always just assumed it would never work. Well you know what they say (something about assuming and Uma Thurman). Thanks for pointing that out.

    The mutable info is still valid though; although not necessarily relevant either. But please ignore the first part of my post and that example code. :o

    What line are you getting that error message on?
    OMG RAVER CHICKS!!
    On a related note: C/C++ Programming Tutorials


    "Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
    "Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards
  14. #8
  15. I [heart] Toxic Waste
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Here
    Posts
    423
    Rep Power
    177
    I'm getting the error on the line where I return ptfPhone.jni_startCom();

    Code:
    mobilePhone::com_startCom()
    {
    	// Added for JNI interface with PTF
    	if(comPort == "PTF")
    	{
    		return ptfPhone.jni_startCom();  // error on this line!!!
    
    	}		
    ....
    again the error is...

    error C2662: 'JNI_Interface::jni_startCom' : cannot convert 'this' pointer from 'JNI_Interface' to 'const JNI_Interface &'


    I've checked my stuff and any of the functions that I wrote in the JNI_Interface struct that get called inside of jni_startCom are const.
  16. #9
  17. I [heart] Toxic Waste
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Here
    Posts
    423
    Rep Power
    177
    Some more information about my issue that could be helpful....

    On the full output of my build I get this not under my issue....

    Code:
    error C2662: 'JNI_Interface::jni_startCom' : cannot convert 'this' pointer from 'JNI_Interface' to 'JNI_Interface &'
            An object from the gc heap (member of a managed class) cannot be converted to an non-gc reference
    so there seems to be an issue with garbage collected and non-gc things?
  18. #10
  19. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,812
    Rep Power
    439
    Originally Posted by DorkRawk
    so there seems to be an issue with garbage collected and non-gc things?
    Hmmm maybe... honestly I don't see anything wrong with your code either. Personally, I don't know much about the gc stuff besides the links I gave you earlier... sorry :( .

    Edit: There is possibly a solution here as well.
    Last edited by peenie; July 26th, 2006 at 06:43 PM. Reason: new link
    OMG RAVER CHICKS!!
    On a related note: C/C++ Programming Tutorials


    "Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
    "Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards

IMN logo majestic logo threadwatch logo seochat tools logo