Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1080
  • Last Modified:

Compile Error

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
chillu71
Asked:
chillu71
1 Solution
 
tinchosCommented:
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
 
chillu71Author Commented:
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
 
SteHCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
tinchosCommented:
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
 
itsmeandnobodyelseCommented:
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
 
chillu71Author Commented:
Thank You
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now