Solved

Compile Error

Posted on 2003-11-24
6
1,053 Views
Last Modified: 2007-12-19
I get following error when I compile my vc++ app.
error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'const char' (or there is no acceptable conversion)

Following is the code snippet:

oNode = objDOMDoc->selectSingleNode("//MessageSize")
if (oNode!=NULL
{
MessageBox(NULL,"MessageSize Node found","Node", MB_OK);      
}            
//sprintf(sVal,"%ls",oNode->text); //convert BSTR (unsigned * string to const char)
            
MessageBox(NULL,oNode->text,"Message Size", MB_OK);
if ((oNode->text) == '1')
{
MessageBox(NULL,"0","Size", MB_OK);
iLoop = 0; //400B
}
if (oNode->text == '2') //4KB
{
MessageBox(NULL,"10","Size", MB_OK);                  
iLoop = 10;
}
if (oNode->text == '3') //40KB
{
MessageBox(NULL,"100","Size", MB_OK);                  
iLoop = 100;
}
            
0
Comment
Question by:chillu71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9811059
I would say that your problem is that you're comparing the return value of the text member (probably a char * or a string) and this cannot be compared to just a char.

Try instead using "1" so that you have not char 1, but char * or string "1"

Hope this helps

Tincho



0
 

Author Comment

by:chillu71
ID: 9811137
When I change if condition to the following on all three ifs
(oNode->text == "2") I get following error

error C2666: '==' : 3 overloads have similar conversions
0
 
LVL 13

Expert Comment

by:SteH
ID: 9811143
You can compare to the first element of oNode->text as well, which is a char. In that case use a switch statement:

switch (oNode->text[0])
{
    case '1':
      MessageBox(NULL,"0","Size", MB_OK);
      iLoop = 0; //400B
      break;
    case '2':
      ....
     
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:tinchos
ID: 9811172
Ok chillu, tell me what type is oNode->text

if it is a string then you can

if ( (oNode->text) == new string("1"))
{
MessageBox(NULL,"0","Size", MB_OK);
iLoop = 0; //400B
}

if it is a char * then you can


if ( *(oNode->text) == '1' )
{
MessageBox(NULL,"0","Size", MB_OK);
iLoop = 0; //400B
}


0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 50 total points
ID: 9811234
The statement

     if ((oNode->text) == "1")

would compile but is always false, because the pointers never are equal.

Next is that oNode->text seems to be a BSTR (mostly wchar_t*) and right side would be const char*, so you have to get the same type at both sides of comparision.
Try this:

#include <comdef.h>

    ....

    if (_bstr_t(oNode->text) == _bstr_t("1"))
    {
    }

That converts both sides to class _bstr_t that has an operator== function to compare string values.

Hope that helps
Alex



0
 

Author Comment

by:chillu71
ID: 9811263
Thank You
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question