473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 5875
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Darkay Li | last post by:
I have try to create an allocator like follow: // the type of allocator must be some kind of pointer template <class Type> class PointerAllocator : public std::allocator<Type> { public: typedef...
3
by: dushkin | last post by:
Hi, This is a member function I wrote: bool MyClass::operator==(const MyClass &a_suaDateTime) const { return (m_oleDateTime == a_suaDateTime.DateTime()); //m_oleDateTime an MFC class } I...
2
by: darkhonor | last post by:
I'm very new to Windows development, all of my background is in Linux, so please bear with me. I am working on a class project and just for fun I'm building a frontend using VC++ 2003. The...
1
by: Bern McCarty | last post by:
I am using MEC++ in VC 7.1. I had a method on a __gc object that looked like this: __property System::UInt32 get_MyProperty(void) { System::Byte __pin * pinBytes = &m_byteArray; // entire...
4
by: nmrcarl | last post by:
I'm trying to upgrade a large project from VS 6.0 to VS 2005. After fixing a lot of things that changed (mostly sloppy coding in the original project that VS2005 didn't allow), I got the release...
2
by: Qasimtn | last post by:
Error occured while compiling the function which is a operator(<) function if remove the key word const in the argument no error occurs. Help me i have no idea what i have done wrong with this...
3
by: mishink7 | last post by:
i am getting this error error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &' with and ...
3
by: =?Utf-8?B?RGlwZXNoX1NoYXJtYQ==?= | last post by:
Hi all, I am porting my code in VC++ to VC.net i.e managed. I have seen somewhere that i need to convert my char* to String*, but String * doesnt perform pointer arithmetic like String* p; *p++='...
1
TheMan1
by: TheMan1 | last post by:
Hi, I'm making a simple program in C++. I have an object named TextReader, with a function GetString(), and I also have another object, TextWriter. Now when *TextWriter is called, it needs to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.