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

D

dorkrawk

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....
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;
};

here are the function defs...
Code:
JNI_Interface::jni_startCom() const
{
	const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
// class of phone to use
.....
}

here is the call...

Code:
in mobilePhone class...
private:
   JNI_Interface ptfPhone;

mobilePhone::com_startCom()
{
	// Added for JNI interface with PTF
	if(comPort == "PTF")
	{
		int teh_test = ptfPhone.jni_startCom();  // for testing
		return 0;

	}

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?
 
V

Victor Bazarov

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....
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;
};

here are the function defs...
Code:
JNI_Interface::jni_startCom() const
{
const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
// class of phone to use
....
}

here is the call...

Code:
in mobilePhone class...
private:
JNI_Interface ptfPhone;

mobilePhone::com_startCom()
{
// Added for JNI interface with PTF
if(comPort == "PTF")
{
int teh_test = ptfPhone.jni_startCom();  // for testing
return 0;

}

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
 
D

dorkrawk

The error is thrown on the call to jni_startCom();

Code:
mobilePhone::com_startCom()
{
	// Added for JNI interface with PTF
	if(comPort == "PTF")
	{
		int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
ERROR!!!
		return 0;

	}
 
A

Andrey Tarasevich

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....
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;
};

here are the function defs...
Code:
JNI_Interface::jni_startCom() const
{
	const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
// class of phone to use
....
}

here is the call...

Code:
in mobilePhone class...
private:
JNI_Interface ptfPhone;

mobilePhone::com_startCom()
{
	// Added for JNI interface with PTF
	if(comPort == "PTF")
	{
		int teh_test = ptfPhone.jni_startCom();  // for testing
		return 0;

	}

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).
 
V

Victor Bazarov

The error is thrown on the call to jni_startCom();

Code:
mobilePhone::com_startCom()
{
// Added for JNI interface with PTF
if(comPort == "PTF")
{
int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
ERROR!!!
return 0;

}

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
 
D

dorkrawk

Victor said:
The error is thrown on the call to jni_startCom();

Code:
mobilePhone::com_startCom()
{
// Added for JNI interface with PTF
if(comPort == "PTF")
{
int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
ERROR!!!
return 0;

}

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

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.
 
B

Bo Persson

Victor said:
The error is thrown on the call to jni_startCom();

Code:
mobilePhone::com_startCom()[/QUOTE][/QUOTE][/QUOTE]

Does this function have a return type?
[QUOTE][QUOTE][QUOTE]
{
// Added for JNI interface with PTF
if(comPort == "PTF")
{
int teh_test = ptfPhone.jni_startCom();  // THIS LINE THROWS THE
ERROR!!!
return 0;

}

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

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
 
N

Noah Roberts

Code:
JNI_Interface::jni_startCom() const
{
	const char* phone_class_name = "phonetest/phone/synergy/SynergyPhone";
// class of phone to use
....
}
Any ideas?

You seem to have a syntax error in your code. "...." will not compile.
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

[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
 
A

Andrey Tarasevich

Kirit said:
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top