Solved

Compile Error

Posted on 2003-11-24
6
1,036 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

809 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