468,512 Members | 1,360 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

VC++ error C2662: cannon convert 'this' pointer...

I am having an issue with some VC++ I am writing. I have a struct and
I'm trying to call a function from it from a function in another
object.

here is the struct....
Expand|Select|Wrap|Line Numbers
  1. struct JNI_Interface
  2. {
  3. JavaVM *jvm;       /* denotes a Java VM */
  4. JNIEnv *env;       /* pointer to native method interface */
  5. //static jobject phone_obj;   // phone object to make JNI calls to PTF
  6. with
  7.  
  8. int jni_startJVM();
  9. int jni_checkException(char* jni_call) const;
  10. int jni_startCom() const;
  11. };
  12.  
here are the function defs...
Expand|Select|Wrap|Line Numbers
  1. JNI_Interface::jni_startCom() const
  2. {
  3. const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
  4. // class of phone to use
  5. .....
  6. }
  7.  
here is the call...

Expand|Select|Wrap|Line Numbers
  1. in mobilePhone class...
  2. private:
  3. JNI_Interface ptfPhone;
  4.  
  5. mobilePhone::com_startCom()
  6. {
  7. // Added for JNI interface with PTF
  8. if(comPort == "PTF")
  9. {
  10. int teh_test = ptfPhone.jni_startCom();  // for testing
  11. return 0;
  12.  
  13. }
  14.  
I am getting this error...

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

all the other online research I've done told me that if I changed these
functions to const that would fix this issue, but it still remains.
Any ideas?

Jul 26 '06 #1
10 5453
do******@gmail.com wrote:
I am having an issue with some VC++ I am writing. I have a struct and
I'm trying to call a function from it from a function in another
object.

here is the struct....
Expand|Select|Wrap|Line Numbers
  1. struct JNI_Interface
  2. {
  3. JavaVM *jvm;       /* denotes a Java VM */
  4. JNIEnv *env;       /* pointer to native method interface */
  5. //static jobject phone_obj;   // phone object to make JNI calls to PTF
  6. with
  7. int jni_startJVM();
  8. int jni_checkException(char* jni_call) const;
  9. int jni_startCom() const;
  10. };
  11.  

here are the function defs...
Expand|Select|Wrap|Line Numbers
  1. JNI_Interface::jni_startCom() const
  2. {
  3. const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
  4. // class of phone to use
  5. ....
  6. }
  7.  

here is the call...

Expand|Select|Wrap|Line Numbers
  1. in mobilePhone class...
  2. private:
  3.   JNI_Interface ptfPhone;
  4. mobilePhone::com_startCom()
  5. {
  6. // Added for JNI interface with PTF
  7. if(comPort == "PTF")
  8. {
  9. int teh_test = ptfPhone.jni_startCom();  // for testing
  10. return 0;
  11. }
  12.  

I am getting this error...

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

all the other online research I've done told me that if I changed
these functions to const that would fix this issue, but it still
remains. Any ideas?
Not enough information. Which line in the posted fragments does
the error message refer to?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '06 #2
The error is thrown on the call to jni_startCom();

Expand|Select|Wrap|Line Numbers
  1. mobilePhone::com_startCom()
  2. {
  3. // Added for JNI interface with PTF
  4. if(comPort == "PTF")
  5. {
  6. int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
  7. ERROR!!!
  8. return 0;
  9.  
  10. }
  11.  
Jul 26 '06 #3
do******@gmail.com wrote:
I am having an issue with some VC++ I am writing. I have a struct and
I'm trying to call a function from it from a function in another
object.

here is the struct....
Expand|Select|Wrap|Line Numbers
  1. struct JNI_Interface
  2. {
  3.     JavaVM *jvm;       /* denotes a Java VM */
  4.     JNIEnv *env;       /* pointer to native method interface */
  5.     //static jobject phone_obj;   // phone object to make JNI calls to PTF
  6. with
  7.     int jni_startJVM();
  8.     int jni_checkException(char* jni_call) const;
  9.     int jni_startCom() const;
  10. };
  11.  

here are the function defs...
Expand|Select|Wrap|Line Numbers
  1. JNI_Interface::jni_startCom() const
  2. {
  3.     const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
  4. // class of phone to use
  5. ....
  6. }
  7.  

here is the call...

Expand|Select|Wrap|Line Numbers
  1. in mobilePhone class...
  2. private:
  3.    JNI_Interface ptfPhone;
  4. mobilePhone::com_startCom()
  5. {
  6.     // Added for JNI interface with PTF
  7.     if(comPort == "PTF")
  8.     {
  9.         int teh_test = ptfPhone.jni_startCom();  // for testing
  10.         return 0;
  11.     }
  12.  

I am getting this error...

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

all the other online research I've done told me that if I changed these
functions to const that would fix this issue, but it still remains.
Any ideas?
Firstly, which line of your code gives you this error exactly? You forgot to
indicate that.

Secondly, are you sure you reproduced the actual error message correctly?
Because frankly the way it looks in your message it doesn't make much sense
(could be VC++ compiler's fault, though).

--
Best regards,
Andrey Tarasevich
Jul 26 '06 #4
do******@gmail.com wrote:
The error is thrown on the call to jni_startCom();

Expand|Select|Wrap|Line Numbers
  1. mobilePhone::com_startCom()
  2. {
  3. // Added for JNI interface with PTF
  4. if(comPort == "PTF")
  5. {
  6. int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
  7. ERROR!!!
  8. return 0;
  9. }
  10.  
So, is 'ptfPhone' "const" or not? Is 'jni_startCom' member function
"const" or not? Read the FAQ 5.8 to understand why I am asking.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '06 #5

Victor Bazarov wrote:
do******@gmail.com wrote:
The error is thrown on the call to jni_startCom();

Expand|Select|Wrap|Line Numbers
  1.  mobilePhone::com_startCom()
  2.  {
  3.  // Added for JNI interface with PTF
  4.  if(comPort == "PTF")
  5.  {
  6.  int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
  7.  ERROR!!!
  8.  return 0;
  9.  
  10.  }
  11.  

So, is 'ptfPhone' "const" or not? Is 'jni_startCom' member function
"const" or not? Read the FAQ 5.8 to understand why I am asking.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I guess the struct and the member function don't need to be "const",
that was just one way of fixing the problem I was having that was
suggested elsewhere online. I took out the "const" from all of them.

Another thing I noticed is that in the output I'm getting this at my
error...

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

And I'm not sure what FAQ you are referring to.

Jul 26 '06 #6
do******@gmail.com wrote:
[..]

And I'm not sure what FAQ you are referring to.
http://www.parashift.com/c++-faq-lite/

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '06 #7

<do******@gmail.comskrev i meddelandet
news:11**********************@p79g2000cwp.googlegr oups.com...
>
Victor Bazarov wrote:
>do******@gmail.com wrote:
The error is thrown on the call to jni_startCom();

Expand|Select|Wrap|Line Numbers
  1. mobilePhone::com_startCom()
Expand|Select|Wrap|Line Numbers
  1.  
  2. Does this function have a return type?
  3.  
  4.         
  5.                         
  6.                         
  7.                 {
  8. // Added for JNI interface with PTF
  9. if(comPort == "PTF")
  10. {
  11. int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
  12. ERROR!!!
  13. return 0;
  14. }
  15.  
  16.  
>>
So, is 'ptfPhone' "const" or not? Is 'jni_startCom' member
function
"const" or not? Read the FAQ 5.8 to understand why I am asking.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

I guess the struct and the member function don't need to be "const",
that was just one way of fixing the problem I was having that was
suggested elsewhere online. I took out the "const" from all of
them.

Another thing I noticed is that in the output I'm getting this at my
error...

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
Are you compiling with the /clr switch set by any chance? If so, you
are using the language called C++/CLI, which is totally different.
Bo Persson
Jul 27 '06 #8

do******@gmail.com wrote:
Expand|Select|Wrap|Line Numbers
  1. JNI_Interface::jni_startCom() const
  2. {
  3.     const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
  4. // class of phone to use
  5. ....
  6. }
  7.  
Any ideas?
You seem to have a syntax error in your code. "...." will not compile.

Jul 27 '06 #9

do******@gmail.com wrote:
[error]
error C2662: 'JNI_Interface::jni_startCom' : cannot convert 'this'
pointer from 'JNI_Interface' to 'const JNI_Interface &'
[/error]

Normally MSVC gives that error if you try to call a non-const function
using a const object. You're doing something like the below:
struct A {
int foo() const;
int bar();
};

void func() {
const A a1;
A a2;

a2.foo(); // ok
a2.bar(); // ok
a1.foo(); // ok
a1.bar(); // error
}
K

Jul 27 '06 #10
Kirit Sælensminde wrote:
do******@gmail.com wrote:
>[error]
error C2662: 'JNI_Interface::jni_startCom' : cannot convert 'this'
pointer from 'JNI_Interface' to 'const JNI_Interface &'
[/error]


Normally MSVC gives that error if you try to call a non-const function
using a const object. You're doing something like the below:
struct A {
int foo() const;
int bar();
};

void func() {
const A a1;
A a2;

a2.foo(); // ok
a2.bar(); // ok
a1.foo(); // ok
a1.bar(); // error
}
...
Not exactly. If you look closer at the error message VC++ generates for your code

***
error C2662: 'bar' : cannot convert 'this' pointer from 'const struct A' to
'struct A &' Conversion loses qualifiers
***

you'll notice that it complains about not being able to convert const-qualified
type to non-const qualified type. This makes sense and this is what is expected
in this case.

The conversion in the OP's error message is in opposite direction: from
non-const qualified type to const qualified type. That's something completely
different.

--
Best regards,
Andrey Tarasevich
Jul 28 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Darkay Li | last post: by
3 posts views Thread by dushkin | last post: by
3 posts views Thread by =?Utf-8?B?RGlwZXNoX1NoYXJtYQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.