Solved

Compile Error

Posted on 2003-11-24
6
1,030 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
6 Comments
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now